Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add functions to test properties of default Lua metamethods #33

Open
ligurio opened this issue Jun 4, 2021 · 1 comment
Open

Add functions to test properties of default Lua metamethods #33

ligurio opened this issue Jun 4, 2021 · 1 comment

Comments

@ligurio
Copy link
Contributor

ligurio commented Jun 4, 2021

Lua has a number of default metamethods (Lua 5.3 Manual -> 2.4 – Metatables and Metamethods) that defines an object behaviour for example for default Lua operators. Most of these metamethods can be covered by general properties. For example if a and b have metamethod __eq then (a == b) == (b == a) and (a == b) ~= (a ~= b), for metamethod __lt - (a > b) == (b < a) and so on. I believe it would simplify testing a bit if lua-quickcheck will discover available metamethods and them with such properties.

What do you think, @luc-tielen? I'm ready to spend some time implementing this.

General methods

  • __index - none?
  • __newindex - none?
  • __mode - none?
  • __call - none?
  • __metatable - none?
  • __tostring
type(tostring(a)) == 'string'
  • __len
type(#a) == 'number' 
  • __pairs - none?
  • __ipairs - none?
  • __gc - none?
  • __name
type(a.__name() == 'string'
  • __close

Mathematic Operators

  • __unm
-(-a) == a
  • __add
a + b == b + a
a + 0 == a
  • __sub - none?
  • __mul
a * 2 == a + a
a * 1 == a * 1 * 1
  • __div - none?
  • __idiv - none?
  • __mod
a % b == a - math.floor(a / b) * b
  • __pow
  • __concat - none?

Bitwise Operators

  • __band
(a & (a - 1)) == 0, if a is x^2
  • __bor
a | 0 == a
  • __bxor
a && b == b && a
  • __bnot
~(~a) == a
  • __shl
a << 0 == a
  • __shr
a >> 0 == a

Equivalence Comparison Operators

  • __eq
(a == b) == (b == a)
(a == b) ~= (a ~= b)
  • __lt
(a > b) == (b < a)
  • __le
(a >= b) == (b <= a)
@ligurio ligurio changed the title Properties for default metamethods Add functions to test properties of default Lua metamethods Jun 4, 2021
@luc-tielen
Copy link
Owner

Hi @ligurio , thanks for the suggestion.

I'm not so sure about the general methods, but I think the rest could be interesting to provide.
I wouldn't automatically do it though, since most of these operations might not make sense, and how would users fix it then?
But maybe we could provide some functions that take X generators, and then set up a default property that is often used?
For example: property_commutative_addition(generator1, generator2)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants