aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/enums.tm37
-rw-r--r--test/import.tm2
-rw-r--r--test/integers.tm21
-rw-r--r--test/optionals.tm20
-rw-r--r--test/paths.tm22
-rw-r--r--test/text.tm8
-rw-r--r--test/values.tm55
7 files changed, 150 insertions, 15 deletions
diff --git a/test/enums.tm b/test/enums.tm
index fe767ebf..aab1b234 100644
--- a/test/enums.tm
+++ b/test/enums.tm
@@ -1,4 +1,5 @@
enum Foo(Zero, One(x:Int), Two(x:Int, y:Int), Three(x:Int, y:Text, z:Bool), Four(x,y,z,w:Int), Last(t:Text))
+enum OnlyTags(A, B, C, D)
func choose_text(f:Foo->Text)
>> f
@@ -33,10 +34,6 @@ func main()
assert Foo.One(123) == Foo.One(123)
assert Foo.Two(123, 456) == Foo.Two(x=123, y=456)
- >> one := Foo.One(123)
- assert one.One == yes
- assert one.Two == no
-
assert Foo.One(10) == Foo.One(10)
assert Foo.One(10) == Foo.Zero == no
@@ -99,3 +96,35 @@ func main()
assert EnumFields(A) == EnumFields(x=A)
+ do
+ e := OnlyTags.A
+ assert e.A == OnlyTags.A.A
+ assert e.B == none
+
+ do
+ e := Foo.Zero
+ assert e.Zero == Foo.Zero.Zero
+ assert e.One == none
+ assert e.Two == none
+
+ ep := @Foo.Zero
+ assert ep.Zero == Foo.Zero.Zero
+ assert ep.One == none
+ assert ep.Two == none
+
+ do
+ e := Foo.Two(123, 456)
+ assert e.Zero == none
+ assert e.One == none
+ assert e.Two != none
+
+ ep := Foo.Two(123, 456)
+ assert ep.Zero == none
+ assert ep.One == none
+ assert ep.Two != none
+
+ two := e.Two!
+ when e is Two(x,y)
+ assert two.x == x
+ assert two.y == y
+ else fail("Unreachable")
diff --git a/test/import.tm b/test/import.tm
index cb642ac6..e05d7e4f 100644
--- a/test/import.tm
+++ b/test/import.tm
@@ -9,9 +9,11 @@ func returns_imported_type(->ImportedType)
func main()
>> empty : [vectors.Vec2]
+ assert empty == []
assert returns_vec() == Vec2(x=1, y=2)
>> imported : [ImportedType]
+ assert imported == []
assert returns_imported_type() == ImportedType("Hello")
assert needs_initializing == 999999999999999999
diff --git a/test/integers.tm b/test/integers.tm
index 1b1f7569..67175f7a 100644
--- a/test/integers.tm
+++ b/test/integers.tm
@@ -105,3 +105,24 @@ func main()
assert Int64(6).get_bit(2) == yes
assert Int64(6).get_bit(3) == yes
assert Int64(6).get_bit(4) == no
+
+ assert Int.parse("123") == 123
+ assert Int.parse("0x10") == 16
+ assert Int.parse("0o10") == 8
+ assert Int.parse("0b10") == 2
+ assert Int.parse("abc") == none
+
+ assert Int.parse("-123") == -123
+ assert Int.parse("-0x10") == -16
+ assert Int.parse("-0o10") == -8
+ assert Int.parse("-0b10") == -2
+
+ for base in (2).to(36)
+ assert Int.parse("10", base=base) == base
+
+ assert Int.parse("111", base=1) == 3
+
+ assert Int.parse("z", base=36) == 35
+ assert Int.parse("Z", base=36) == 35
+ assert Int.parse("-z", base=36) == -35
+ assert Int.parse("-Z", base=36) == -35
diff --git a/test/optionals.tm b/test/optionals.tm
index f439d998..53741748 100644
--- a/test/optionals.tm
+++ b/test/optionals.tm
@@ -61,6 +61,12 @@ func maybe_c_string(should_i:Bool->CString?)
else
return none
+func maybe_path(should_i:Bool->Path?)
+ if should_i
+ return (./foo)
+ else
+ return none
+
func main()
optional : Int? = 5
assert optional == 5
@@ -214,6 +220,20 @@ func main()
fail("Truthy: $nope")
else say("Falsey: $nope")
+ do
+ say("...")
+ say("Paths:")
+ yep := maybe_path(yes)
+ assert yep == (./foo)
+ nope := maybe_path(no)
+ assert nope == none
+ >> if yep
+ assert yep == (./foo)
+ else fail("Falsey: $yep")
+ >> if nope
+ fail("Truthy: $nope")
+ else say("Falsey: $nope")
+
if yep := maybe_int(yes)
assert yep == 123
else fail("Unreachable")
diff --git a/test/paths.tm b/test/paths.tm
index bcda8e1f..6622847e 100644
--- a/test/paths.tm
+++ b/test/paths.tm
@@ -17,8 +17,8 @@ func main()
assert optional_path == (./foo)
>> tmpfile := (tmpdir++(./one.txt))
- >> tmpfile.write("Hello world")
- >> tmpfile.append("!")
+ >> tmpfile.write("Hello world")!
+ >> tmpfile.append("!")!
assert tmpfile.read() == "Hello world!"
assert tmpfile.read_bytes()! == [0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x77, 0x6F, 0x72, 0x6C, 0x64, 0x21]
assert tmpdir.files().has(tmpfile)
@@ -30,16 +30,16 @@ func main()
assert (./does-not-exist.xxx).read() == none
assert (./does-not-exist.xxx).read_bytes() == none
- if lines := (./does-not-exist.xxx).by_line() then
+ if (./does-not-exist.xxx).by_line()
fail("I could read lines in a nonexistent file")
else
pass
- >> tmpfile.remove()
+ >> tmpfile.remove()!
assert tmpdir.files().has(tmpfile) == no
- >> tmpdir.remove()
+ >> tmpdir.remove()!
>> p := (/foo/baz.x/qux.tar.gz)
assert p.base_name() == "qux.tar.gz"
@@ -59,12 +59,12 @@ func main()
assert (~/.foo.baz.qux).extension() == "baz.qux"
- assert (/).parent() == (/)
assert (~/x/.).parent() == (~)
assert (~/x).parent() == (~)
assert (.).parent() == (..)
assert (..).parent() == (../..)
assert (../foo).parent() == (..)
+ assert (/).parent() == none
# Concatenation tests:
say("Basic relative path concatenation:")
@@ -103,8 +103,8 @@ func main()
say("Globbing:")
>> (./*.tm).glob()
- assert (./foo).type == Relative
- assert (/foo).type == Absolute
- assert (~/foo).type == Home
- assert (/foo/baz).components == ["foo", "baz"]
- assert Path(type=Relative, ["foo", "baz"]) == (./foo/baz)
+ assert (./foo).RelativePath
+ assert (/foo).AbsolutePath
+ assert (~/foo).HomePath
+ assert (/foo/baz).components() == ["foo", "baz"]
+ assert Path.RelativePath(["foo", "baz"]) == (./foo/baz)
diff --git a/test/text.tm b/test/text.tm
index 6631b94e..6c23042d 100644
--- a/test/text.tm
+++ b/test/text.tm
@@ -57,6 +57,8 @@ func main()
assert "xxxx".replace("x", "") == ""
assert "xxxx".replace("y", "") == "xxxx"
assert "One two three four five six".replace("e ", "") == "Ontwo threfour fivsix"
+ assert "Hello".replace("", "xxx") == "Hello"
+ assert "".replace("", "xxx") == ""
assert amelie.has(amelie2) == yes
@@ -200,3 +202,9 @@ func main()
assert Text.from_utf32([150370]) == test
assert Text.from_utf16([-10158, -8350]) == test
assert Text.from_utf8([0xf0, 0xa4, 0xad, 0xa2]) == test
+
+
+ assert "one two".find("one") == 1
+ assert "one two".find("two") == 5
+ assert "one two".find("three") == none
+ assert "one two".find("o", start=2) == 7
diff --git a/test/values.tm b/test/values.tm
new file mode 100644
index 00000000..9f86c012
--- /dev/null
+++ b/test/values.tm
@@ -0,0 +1,55 @@
+# Tests for ensuring immutable value nature in various contexts
+struct Inner(xs:[Int32])
+
+struct Outer(inner:Inner)
+
+enum HoldsList(HasList(xs:[Int32]))
+
+func sneaky(outer:Outer)
+ (&outer.inner.xs)[1] = 99
+
+func sneaky2(outer:&Outer)
+ (&outer.inner.xs)[1] = 99
+
+func main()
+ do
+ xs := [10, 20, 30]
+ copy := xs
+ (&xs)[1] = 99
+ assert xs == [99, 20, 30]
+ assert copy == [10, 20, 30]
+
+ do
+ t := {"A":10, "B":20}
+ copy := t
+ (&t)["A"] = 99
+ assert t == {"A":99, "B":20}
+ assert copy == {"A":10, "B":20}
+
+ do
+ foo := Outer(Inner([10, 20, 30]))
+ copy := foo
+ (&foo.inner.xs)[1] = 99
+ assert foo.inner.xs == [99, 20, 30]
+ assert copy.inner.xs == [10, 20, 30]
+
+ do
+ foo := Outer(Inner([10, 20, 30]))
+ copy := foo
+ sneaky(foo)
+ assert foo.inner.xs == [10, 20, 30]
+ assert copy.inner.xs == [10, 20, 30]
+
+ do
+ foo := Outer(Inner([10, 20, 30]))
+ copy := foo
+ sneaky2(&foo)
+ assert foo.inner.xs == [99, 20, 30]
+ assert copy.inner.xs == [10, 20, 30]
+
+ do
+ x := HoldsList.HasList([10, 20, 30])
+ when x is HasList(list)
+ (&list)[1] = 99
+
+ assert x == HoldsList.HasList([10, 20, 30])