]> gitweb.factorcode.org Git - factor.git/commitdiff
bare: check enum values when reading and writing.
authorJohn Benediktsson <mrjbq7@gmail.com>
Thu, 16 Jun 2022 16:40:12 +0000 (09:40 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Thu, 16 Jun 2022 16:40:12 +0000 (09:40 -0700)
extra/bare/bare-tests.factor
extra/bare/bare.factor

index de739c22b07cdcfd0c65904f50efa38123f56975..8ae502f4c49901bc81809235535e22570896b3f3 100644 (file)
@@ -148,6 +148,9 @@ IN: bare.tests
 { "BUZZ" } [ B{ 0x80 0x02 } T{ enum f { { "FOO" 0 } { "BAR" 255 } { "BUZZ" 256 } } } bare> ] unit-test
 { B{ 0x80 0x02 } } [ "BUZZ" T{ enum f { { "FOO" 0 } { "BAR" 255 } { "BUZZ" 256 } } } >bare ] unit-test
 
+[ B{ 0x03 } T{ enum f { { "A" 0 } { "B" 1 } { "C" 2 } } } bare> ] [ invalid-enum? ] must-fail-with
+[ "D" T{ enum f { { "A" 0 } { "B" 1 } { "C" 2 } } } >bare ] [ invalid-enum? ] must-fail-with
+
 ! optional<u32>
 
 { f } [ B{ 0x00 } T{ optional f u32 } bare> ] unit-test
index 429fdfa83c153cad8ef79b7330b2dba76a9002fb..c4eb438552b575bc7e0df64f0ef8201e673a0f9d 100644 (file)
@@ -9,6 +9,8 @@ strings words words.constant ;
 
 IN: bare
 
+ERROR: invalid-enum value ;
+
 SINGLETONS: uint ;
 SINGLETONS: int ;
 SINGLETONS: u8 u16 u32 u64 ;
@@ -59,7 +61,8 @@ M: data write-bare
 
 M: void write-bare 2drop ;
 
-M: enum write-bare values>> at uint write-bare ;
+M: enum write-bare
+    values>> ?at [ uint write-bare ] [ invalid-enum ] if ;
 
 M: optional write-bare
     over 1 0 ? u8 write-bare
@@ -120,7 +123,9 @@ M: data read-bare
 
 M: void read-bare drop f ; ! XXX: this isn't right
 
-M: enum read-bare [ uint read-bare ] dip values>> value-at ;
+M: enum read-bare
+    [ uint read-bare ] dip values>> ?value-at
+    [ invalid-enum ] unless ;
 
 M: optional read-bare
     u8 read-bare 1 = [ type>> read-bare ] [ drop f ] if ;