diff options
| -rw-r--r-- | core/text.nom | 44 | ||||
| -rw-r--r-- | lib/object.nom | 41 | ||||
| -rw-r--r-- | lib/os.nom | 19 | ||||
| -rwxr-xr-x | tools/test.nom | 6 |
4 files changed, 109 insertions, 1 deletions
diff --git a/core/text.nom b/core/text.nom index 83c25f7..9b6fe06 100644 --- a/core/text.nom +++ b/core/text.nom @@ -5,7 +5,34 @@ use "core/metaprogramming.nom" +test: + assume (("x" + "y") == "xy") + %s = "list:\[1, 2, 3]" + assume (%s == "list:[1, 2, 3]") + assume ("foo = \(1 + 2)!" == "foo = 3!") + assume (..) + ".." + one + two + ..== "one\ntwo" + assume (..) + ".." + no\ + ..gap + ..== "nogap" + parse [アクション %spec %body] as (action %spec %body) + +test: + %こんにちは = "こんにちは" + アクション [% と言う] "\(%)世界" + assume ((%こんにちは と言う) == "こんにちは世界") or barf ".." + Unicode doesn't work + # Text functions +test: + assume ((["x", "y"] joined with ",") == "x,y") or barf "joined with failed" + assume ((["x", "y"] joined) == "xy") or barf "joined failed" + action [%texts joined with %glue] (..) lua> ".." local text_bits = {} @@ -13,9 +40,15 @@ action [%texts joined with %glue] (..) return table.concat(text_bits, \%glue) parse [joined %texts, %texts joined] as (%texts joined with "") +test: + assume (("asdf" capitalized) == "Asdf") or barf "capitalized failed" + compile [capitalized %text, %text capitalized] to (..) Lua value "((\(%text as lua expr)):gsub('%l', string.upper, 1))" +test: + assume (("asdf" with "X" instead of "s") == "aXdf") or barf ".." + substitution failed compile [..] %text with %sub instead of %patt, %text with %patt replaced by %sub %text s/ %patt / %sub @@ -23,6 +56,14 @@ compile [..] Lua value ".." ((\(%text as lua expr)):gsub(\(%patt as lua expr), \(%sub as lua expr))) +test: + assume (..) + (..) + lines in ".." + one + two + ..== ["one", "two"] + action [lines in %text, lines of %text] (..) lua> ".." local result = list{} @@ -56,6 +97,9 @@ compile [%text matches %pattern] to (..) compile [%text matching %pattern] to (..) Lua value "\(%text as lua expr):match(\(%pattern as lua expr))" +test: + assume ("\n" == (newline)) or barf "Text literals failed." + # Text literals lua> ".." do diff --git a/lib/object.nom b/lib/object.nom index 48e602a..e78eb14 100644 --- a/lib/object.nom +++ b/lib/object.nom @@ -4,6 +4,47 @@ use "core" +test: + object "Dog": + (class Dog).genus = "Canus" + method [initialize %] (%.barks or= 0) + method [bark, woof]: + %barks = ("Bark!" for % in 1 to (me).barks) + return (%barks joined with " ") + + method [get pissed off] ((me).barks += 1) + + %d = (new Dog {barks:2}) + as %d: + assume ((me) == %d) + assume ((me).barks == 2) + assume ((bark) == "Bark! Bark!") + assume ((woof) == "Bark! Bark!") + get pissed off + assume ((me).barks == 3) + assume ((bark) == "Bark! Bark! Bark!") + assume ((me).genus == "Canus") + + assume ("\(%d.class)" == "Dog") + assume (%d.genus == "Canus") + assume (%d.barks == 3) + as (new Dog) (assume ((me).barks == 0) or barf "Default initializer failed") + as (new Dog {barks:1}) (assume ((bark) == "Bark!")) + action [foo] (as (new Dog {barks:23}) (return (me).barks)) + assume ((foo) == 23) or barf "Oops, \(foo) != 23" + as (new Dog {barks:101}): + try (as (new Dog {barks:8}) (barf)) and if it succeeds (barf) + assume ((me).barks == 101) or barf ".." + Error in nested 'as % %' failed to properly reset 'self' + + object "Corgi" extends (class Dog) (method [sploot] "splooted") + %corg = (new Corgi) + assume (%corg.barks == 0) + as (new Corgi {barks:1}): + assume ((sploot) == "splooted") or barf "subclass method failed" + assume ((bark) == "Bark!") or barf "inheritance failed" + assume ((woof) == "Bark!") + compile [@, me] to (Lua value "self") compile [method %actions %body] to: %lua = (compile as (action %actions %body)) @@ -4,6 +4,9 @@ use "core" +test: + path of Nomsu file "lib/os.nom" + action [path of Nomsu file %filename]: lua> "for i,f in Files.walk(\%filename) do return f end" barf "Could not find file: \%filename" @@ -15,7 +18,13 @@ action [sh> %cmd]: result:close() return contents +test: + read file "lib/os.nom" + action [read file %filename] (=lua "Files.read(\%filename)") +test: + for file %f in "core": do nothing + compile [for file %f in %path %body] to (..) Lua ".." for i,\(%f as lua expr) in Files.walk(\(%path as lua expr)) do @@ -43,8 +52,18 @@ action [..] file:write(\%text) file:close() +test: + assume ((line number of 3 in "x\ny") == 2) + action [line number of %pos in %str] (=lua "Files.get_line_number(\%str, \%pos)") +test: + assume ((line 2 in "one\ntwo\nthree") == "two") + action [line %line_num in %str] (=lua "Files.get_line(\%str, \%line_num)") + +test: + assume (source lines of \(this)) + action [source lines of %tree]: %source = (%tree.source if (%tree is syntax tree) else %tree) %file = (read file %source.filename) diff --git a/tools/test.nom b/tools/test.nom index a80ea55..3f2622e 100755 --- a/tools/test.nom +++ b/tools/test.nom @@ -8,9 +8,13 @@ if (%args.1 == "-v"): remove index 1 from %args %verbose = (yes) +# Make sure all the files get run +for %path in (command line args): + for file %filename in %path: + if (%filename matches "%.nom$"): use %filename + %tests = ((=lua "Source:from_string(\%s)") = %t for %s = %t in (tests)) for %path in (command line args): - if (%path is "-i"): %inplace = (yes) for file %filename in %path: unless (%filename matches "%.nom$"): do next %filename %file_tests = [] |
