diff test/SbpOperators/readoperator_test.jl @ 831:760c11e81fd4 operator_storage_array_of_table

Introduce parse_tuple and parse_scalar and replace all external calls to parse_rational
author Jonatan Werpers <jonatan@werpers.com>
date Wed, 12 Jan 2022 15:32:58 +0100
parents 21ab60cc0a5c
children 06c510d40ebb
line wrap: on
line diff
--- a/test/SbpOperators/readoperator_test.jl	Wed Jan 12 14:59:03 2022 +0100
+++ b/test/SbpOperators/readoperator_test.jl	Wed Jan 12 15:32:58 2022 +0100
@@ -5,25 +5,6 @@
 
 import Sbplib.SbpOperators.Stencil
 
-
-@testset "parse_rational" begin
-    @test SbpOperators.parse_rational("1") isa Rational
-    @test SbpOperators.parse_rational("1") == 1//1
-    @test SbpOperators.parse_rational("1/2") isa Rational
-    @test SbpOperators.parse_rational("1/2") == 1//2
-    @test SbpOperators.parse_rational("37/13") isa Rational
-    @test SbpOperators.parse_rational("37/13") == 37//13
-
-    @test SbpOperators.parse_rational(0.5) isa Rational
-    @test SbpOperators.parse_rational(0.5) == 1//2
-
-    @test SbpOperators.parse_rational("0.5") isa Rational
-    @test SbpOperators.parse_rational("0.5") == 1//2
-
-    @test SbpOperators.parse_rational(2) isa Rational
-    @test SbpOperators.parse_rational(2) == 2//1
-end
-
 @testset "readoperator" begin
     toml_str = """
         [meta]
@@ -126,4 +107,66 @@
         @test parse_stencil(Float64, TOML.parse(toml)["s2"]) == Stencil(2/1, -5/1, 4/1, -1/1, 0/1, 0/1; center=1)
         @test parse_stencil(Float64, TOML.parse(toml)["s3"]) == Stencil(1/1, -2/1, 1/1, 0/1, 0/1, 0/1; center=2)
     end
+
+    @testset "parse_scalar" begin
+        toml = TOML.parse("""
+            a1 = 1
+            a2 = 1.5
+            a3 = 1.0
+            a4 = 10
+            a5 = "1/2"
+            a6 = "1.5"
+
+            e1 = [1,2,3]
+            e2 = "a string value"
+        """)
+
+        @test parse_scalar(toml["a1"]) == 1//1
+        @test parse_scalar(toml["a2"]) == 3//2
+        @test parse_scalar(toml["a3"]) == 1//1
+        @test parse_scalar(toml["a4"]) == 10//1
+        @test parse_scalar(toml["a5"]) == 1//2
+        @test parse_scalar(toml["a6"]) == 3//2
+
+        @test_throws ArgumentError parse_scalar(toml["e1"])
+        @test_throws ArgumentError parse_scalar(toml["e2"])
+    end
+
+    @testset "parse_tuple" begin
+        toml = TOML.parse("""
+            t1 = [1,3,4]
+            t2 = ["1/2","3/4","2/1"]
+
+            e1 = "not a tuple"
+            e2.a="1"
+            e3 = 1
+            e4 = ["1/2","3/4","not a number"]
+        """)
+
+        @test parse_tuple(toml["t1"]) == (1//1,3//1,4//1)
+        @test parse_tuple(toml["t2"]) == (1//2,3//4,2//1)
+
+        @test_throws ArgumentError parse_tuple(toml["e1"])
+        @test_throws ArgumentError parse_tuple(toml["e2"])
+        @test_throws ArgumentError parse_tuple(toml["e3"])
+        @test_throws ArgumentError parse_tuple(toml["e4"])
+    end
 end
+
+@testset "parse_rational" begin
+    @test SbpOperators.parse_rational("1") isa Rational
+    @test SbpOperators.parse_rational("1") == 1//1
+    @test SbpOperators.parse_rational("1/2") isa Rational
+    @test SbpOperators.parse_rational("1/2") == 1//2
+    @test SbpOperators.parse_rational("37/13") isa Rational
+    @test SbpOperators.parse_rational("37/13") == 37//13
+
+    @test SbpOperators.parse_rational(0.5) isa Rational
+    @test SbpOperators.parse_rational(0.5) == 1//2
+
+    @test SbpOperators.parse_rational("0.5") isa Rational
+    @test SbpOperators.parse_rational("0.5") == 1//2
+
+    @test SbpOperators.parse_rational(2) isa Rational
+    @test SbpOperators.parse_rational(2) == 2//1
+end