diff options
Diffstat (limited to 'advtrains/spec')
-rw-r--r-- | advtrains/spec/poconvert_spec.lua | 70 | ||||
-rw-r--r-- | advtrains/spec/speed_spec.lua | 70 | ||||
-rw-r--r-- | advtrains/spec/texture_spec.lua | 19 | ||||
-rw-r--r-- | advtrains/spec/wagons_spec.lua | 40 |
4 files changed, 199 insertions, 0 deletions
diff --git a/advtrains/spec/poconvert_spec.lua b/advtrains/spec/poconvert_spec.lua new file mode 100644 index 0000000..51f33e7 --- /dev/null +++ b/advtrains/spec/poconvert_spec.lua @@ -0,0 +1,70 @@ +package.path = "../?.lua;" .. package.path +advtrains = {} +_G.advtrains = advtrains +local poconvert = require("poconvert") + +describe("PO file converter", function() + it("should convert PO files", function() + assert.equals([[ +# textdomain: foo +foo=bar +baz= +#@=wh\at\\@n=@=w\as\\@n +multiline@nstrings=multiline@nresult +with context?=oder doch nicht]], poconvert.from_string("foo", [[ +msgid "" +msgstr "whatever metadata" + +msgid "foo" +msgstr "bar" + +msgid "baz" +msgstr "" + +#, fuzzy +msgid "=wh\\at\\\\\n" +msgstr "=w\\as\\\\\n" + +msgid "multi" +"line\n" +"strings" +msgstr "multi" +"line\n" +"result" + +msgctxt "i18n context" +msgid "with context?" +msgstr "oder doch nicht"]])) + end) + it("should reject invalid tokens", function() + assert.has.errors(function() + poconvert.from_string("", [[ +foo "" +bar ""]]) + end, "Invalid token: foo") + end) + it("should reject entries without a msgstr", function() + assert.has.errors(function() + poconvert.from_string("", [[msgid "foo"]]) + end, "Missing translated string") + end) + it("should reject entries without a msgid", function() + assert.has.errors(function() + poconvert.from_string("", [[msgstr "foo"]]) + end, "Missing untranslated string") + end) + it("should reject entries with improperly enclosed strings", function() + assert.has.errors(function() + poconvert.from_string("", [[ +msgid "foo" +msgstr "bar \]]) + end, "String extends beyond the end of input") + end) + it("should reject incomplete input", function() + assert.has.errors(function() + poconvert.from_string("", [[ +msgid "foo" +msgstr]]) + end, "No string provided for msgstr") + end) +end) diff --git a/advtrains/spec/speed_spec.lua b/advtrains/spec/speed_spec.lua new file mode 100644 index 0000000..97f8ffa --- /dev/null +++ b/advtrains/spec/speed_spec.lua @@ -0,0 +1,70 @@ +package.path = "../?.lua;" .. package.path +advtrains = {} +_G.advtrains = advtrains +local speed = require("speed") + +describe("Arithmetic functions on speed restrictions", function() + it("should work", function() + local a = math.random() + local b = math.random(20) + -- This test is basically a "typo check" + assert.is_true (speed.lessp(a, b)) + assert.is_false(speed.greaterp(a, b)) + assert.is_false(speed.not_lessp(a, b)) + assert.is_true (speed.not_greaterp(a, b)) + assert.is_false(speed.lessp(a, a)) + assert.is_false(speed.greaterp(a, a)) + assert.is_true (speed.equalp(a, a)) + assert.is_false(speed.not_equalp(a, a)) + assert.equal(b, speed.max(a, b)) + assert.equal(a, speed.min(a, b)) + end) + it("should handle -1", function() + assert.is_false(speed.lessp(-1, math.random())) + end) + it("should handle nil", function() + assert.is_true(speed.greaterp(nil, math.random())) + end) + it("should handle mixed nil and -1", function() + assert.is_true(speed.equalp(nil, -1)) + end) +end) + +describe("The speed restriction setter", function() + it("should set the signal aspect", function() + local t = {speed_restrictions_t = {x = 5, y = 9}} + local u = {speed_restrictions_t = {x = 7, y = 9}, speed_restriction = 7} + speed.merge_aspect(t, {main = 7, type = "x"}) + assert.same(u, t) + end) + it("should work with existing signal aspect tables", function() + local t = {speed_restrictions_t = {main = 5, foo = 3}} + local u = {speed_restrictions_t = {main = 7, foo = 3}, speed_restriction = 3} + speed.merge_aspect(t, {main = 7}) + assert.same(u, t) + end) + it("should work with distant signals", function() + local t = {speed_restrictions_t = {main = 5}} + local u = {speed_restrictions_t = {main = 5}, speed_restriction = 5} + speed.merge_aspect(t, {}) + assert.same(u, t) + end) + it("should create the restriction table if necessary", function() + local t = {speed_restriction = 5} + local u = {speed_restriction = 3, speed_restrictions_t = {main = 5, foo = 3}} + speed.merge_aspect(t, {main = 3, type = "foo"}) + assert.same(u, t) + end) + it("should also create the restriction table for trains without any speed limit", function() + local t = {} + local u = {speed_restrictions_t = {}} + speed.merge_aspect(t, {}) + assert.same(u, t) + end) + it("should set the speed restriction to nil if that is the case", function() + local t = {speed_restriction = math.random(20)} + local u = {speed_restrictions_t = {main = -1}} + speed.merge_aspect(t, {main = -1}) + assert.same(u, t) + end) +end) diff --git a/advtrains/spec/texture_spec.lua b/advtrains/spec/texture_spec.lua new file mode 100644 index 0000000..2e3bd5d --- /dev/null +++ b/advtrains/spec/texture_spec.lua @@ -0,0 +1,19 @@ +package.path = "../?.lua;" .. package.path +local T = require "texture" + +describe("Texture creation", function() + it("works", function() + assert.same("^.png", tostring(T.raw"^.png")) + assert.same("foo\\:bar.png", tostring(T"foo:bar.png")) + end) +end) + +describe("Texture modifiers", function() + it("work", function() + assert.same("x^[colorize:c", tostring(T"x":colorize"c")) + assert.same("x^[colorize:c:alpha", tostring(T"x":colorize("c", "alpha"))) + assert.same("x^[multiply:c", tostring(T"x":multiply"c")) + assert.same("x^[resize:2x3", tostring(T"x":resize(2, 3))) + assert.same("x^[transformI", tostring(T"x":transform"I")) + end) +end) diff --git a/advtrains/spec/wagons_spec.lua b/advtrains/spec/wagons_spec.lua new file mode 100644 index 0000000..df0687b --- /dev/null +++ b/advtrains/spec/wagons_spec.lua @@ -0,0 +1,40 @@ +require "mineunit" +mineunit "core" + +_G.advtrains = { + wagon_load_range = 32 +} +sourcefile "wagons" + +local myproto = {_test = true} +advtrains.register_wagon(":mywagon", myproto, "My wagon", "", false) +advtrains.register_wagon_alias(":myalias", ":mywagon") +advtrains.register_wagon_alias(":myotheralias", ":myalias") + +local myotherproto = {_other = true} +advtrains.register_wagon(":noalias", myotherproto, "Not aliased wagon", "", false) +advtrains.register_wagon_alias(":noalias", ":mywagon") + +advtrains.register_wagon_alias(":nilalias", ":nil") + +advtrains.register_wagon_alias(":R1", ":R2") +advtrains.register_wagon_alias(":R2", ":R3") +advtrains.register_wagon_alias(":R3", ":R1") + +describe("wagon alias system", function() + it("should work", function() + assert.same({":mywagon", myproto}, {advtrains.resolve_wagon_alias(":myalias")}) + assert.equal(myproto, advtrains.wagon_prototypes[":myalias"]) + assert.same({":mywagon", myproto}, {advtrains.resolve_wagon_alias(":myotheralias")}) + end) + it("should respect wagon registration", function() + assert.same({":noalias", myotherproto}, {advtrains.resolve_wagon_alias(":noalias")}) + end) + it("should handle recursive loops", function() + assert.same({}, {advtrains.resolve_wagon_alias(":R1")}) + end) + it("should return nil for missing entries", function() + assert.same({}, {advtrains.resolve_wagon_alias(":what")}) + assert.same({}, {advtrains.resolve_wagon_alias(":nilalias")}) + end) +end) |