1before: | 2 this_module = "std.operator" 3 global_table = "_G" 4 5 M = require (this_module) 6 7specify std.operator: 8- context when required: 9 - context by name: 10 - it does not touch the global table: 11 expect (show_apis {added_to=global_table, by=this_module}). 12 to_equal {} 13 14 - context via the std module: 15 - it does not touch the global table: 16 expect (show_apis {added_to=global_table, by="std"}). 17 to_equal {} 18 19 20- describe concat: 21 - before: 22 f = M.concat 23 24 - it stringifies its arguments: 25 expect (f (1, "")).to_be "1" 26 expect (f ("", 2)).to_be "2" 27 - it concatenates its arguments: 28 expect (f (1, 2)).to_be "12" 29 30- describe get: 31 - before: 32 f = M.get 33 34 - it dereferences a table: 35 expect (f ({}, 1)).to_be (nil) 36 expect (f ({"foo", "bar"}, 1)).to_be "foo" 37 expect (f ({foo = "bar"}, "foo")).to_be "bar" 38 39- describe set: 40 - before: 41 f = M.set 42 43 - it sets a table entry: 44 expect (f ({}, 1, 42)).to_equal {42} 45 expect (f ({}, "foo", 42)).to_equal {foo=42} 46 - it overwrites an existing entry: 47 expect (f ({1, 2}, 1, 42)).to_equal {42, 2} 48 expect (f ({foo="bar", baz="quux"}, "foo", 42)). 49 to_equal {foo=42, baz="quux"} 50 51- describe sum: 52 - before: 53 f = M.sum 54 55 - it returns the sum of its arguments: 56 expect (f (99, 2)).to_be (99 + 2) 57 58- describe diff: 59 - before: 60 f = M.diff 61 62 - it returns the difference of its arguments: 63 expect (f (99, 2)).to_be (99 - 2) 64 65- describe prod: 66 - before: 67 f = M.prod 68 69 - it returns the product of its arguments: 70 expect (f (99, 2)).to_be (99 * 2) 71 72- describe quot: 73 - before: 74 f = M.quot 75 76 - it returns the quotient of its arguments: 77 expect (f (99, 2)).to_be (99 / 2) 78 79- describe mod: 80 - before: 81 f = M.mod 82 83 - it returns the modulus of its arguments: 84 expect (f (99, 2)).to_be (99 % 2) 85 86- describe pow: 87 - before: 88 f = M.pow 89 90 - it returns the power of its arguments: 91 expect (f (99, 2)).to_be (99 ^ 2) 92 93- describe conj: 94 - before: 95 f = M.conj 96 97 - it returns the logical and of its arguments: 98 expect (f (false, false)).to_be (false) 99 expect (f (false, true)).to_be (false) 100 expect (f (true, false)).to_be (false) 101 expect (f (true, true)).to_be (true) 102 - it supports truthy and falsey arguments: 103 expect (f ()).to_be (nil) 104 expect (f (0)).to_be (nil) 105 expect (f (nil, 0)).to_be (nil) 106 expect (f (0, "false")).to_be ("false") 107 108- describe disj: 109 - before: 110 f = M.disj 111 112 - it returns the logical or of its arguments: 113 expect (f (false, false)).to_be (false) 114 expect (f (false, true)).to_be (true) 115 expect (f (true, false)).to_be (true) 116 expect (f (true, true)).to_be (true) 117 - it supports truthy and falsey arguments: 118 expect (f ()).to_be (nil) 119 expect (f (0)).to_be (0) 120 expect (f (nil, 0)).to_be (0) 121 expect (f (0, "false")).to_be (0) 122 123- describe neg: 124 - before: 125 f = M.neg 126 127 - it returns the logical not of its argument: 128 expect (f (false)).to_be (true) 129 expect (f (true)).to_be (false) 130 - it supports truthy and falsey arguments: 131 expect (f ()).to_be (true) 132 expect (f (0)).to_be (false) 133 134- describe eq: 135 - before: 136 f = M.eq 137 138 - it returns true if the arguments are equal: 139 expect (f ()).to_be (true) 140 expect (f ("foo", "foo")).to_be (true) 141 - it returns false if the arguments are unequal: 142 expect (f (1)).to_be (false) 143 expect (f ("foo", "bar")).to_be (false) 144 145- describe neq: 146 - before: 147 f = M.neq 148 149 - it returns false if the arguments are equal: 150 expect (f (1, 1)).to_be (false) 151 expect (f ("foo", "foo")).to_be (false) 152 - it returns true if the arguments are unequal: 153 expect (f (1)).to_be (true) 154 expect (f ("foo", "bar")).to_be (true) 155 expect (f ({}, {})).to_be (true) 156 157- describe lt: 158 - before: 159 f = M.lt 160 161 - it returns true if the arguments are in ascending order: 162 expect (f (1, 2)).to_be (true) 163 expect (f ("a", "b")).to_be (true) 164 - it returns false if the arguments are not in ascending order: 165 expect (f (2, 2)).to_be (false) 166 expect (f (3, 2)).to_be (false) 167 expect (f ("b", "b")).to_be (false) 168 expect (f ("c", "b")).to_be (false) 169 - it supports __lt metamethods: 170 List = require "std.list" {} 171 expect (f (List {1, 2, 3}, List {1, 2, 3, 4})).to_be (true) 172 expect (f (List {1, 2, 3}, List {1, 2, 3})).to_be (false) 173 expect (f (List {1, 2, 4}, List {1, 2, 3})).to_be (false) 174 175- describe lte: 176 - before: 177 f = M.lte 178 179 - it returns true if the arguments are not in descending order: 180 expect (f (1, 2)).to_be (true) 181 expect (f (2, 2)).to_be (true) 182 expect (f ("a", "b")).to_be (true) 183 expect (f ("b", "b")).to_be (true) 184 - it returns false if the arguments are in descending order: 185 expect (f (3, 2)).to_be (false) 186 expect (f ("c", "b")).to_be (false) 187 - it supports __lte metamethods: 188 List = require "std.list" {} 189 expect (f (List {1, 2, 3}, List {1, 2, 3, 4})).to_be (true) 190 expect (f (List {1, 2, 3}, List {1, 2, 3})).to_be (true) 191 expect (f (List {1, 2, 4}, List {1, 2, 3})).to_be (false) 192 193- describe gt: 194 - before: 195 f = M.gt 196 197 - it returns true if the arguments are in descending order: 198 expect (f (2, 1)).to_be (true) 199 expect (f ("b", "a")).to_be (true) 200 - it returns false if the arguments are not in descending order: 201 expect (f (2, 2)).to_be (false) 202 expect (f (2, 3)).to_be (false) 203 expect (f ("b", "b")).to_be (false) 204 expect (f ("b", "c")).to_be (false) 205 - it supports __lt metamethods: 206 List = require "std.list" {} 207 expect (f (List {1, 2, 3, 4}, List {1, 2, 3})).to_be (true) 208 expect (f (List {1, 2, 3}, List {1, 2, 3})).to_be (false) 209 expect (f (List {1, 2, 3}, List {1, 2, 4})).to_be (false) 210 211- describe gte: 212 - before: 213 f = M.gte 214 215 - it returns true if the arguments are not in ascending order: 216 expect (f (2, 1)).to_be (true) 217 expect (f (2, 2)).to_be (true) 218 expect (f ("b", "a")).to_be (true) 219 expect (f ("b", "b")).to_be (true) 220 - it returns false if the arguments are in ascending order: 221 expect (f (2, 3)).to_be (false) 222 expect (f ("b", "c")).to_be (false) 223 - it supports __lte metamethods: 224 List = require "std.list" {} 225 expect (f (List {1, 2, 3, 4}, List {1, 2, 3})).to_be (true) 226 expect (f (List {1, 2, 3}, List {1, 2, 3})).to_be (true) 227 expect (f (List {1, 2, 3}, List {1, 2, 4})).to_be (false) 228