fOOrth 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (155) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +21 -0
  3. data/.rdoc_options +17 -0
  4. data/Gemfile +4 -0
  5. data/README.md +67 -0
  6. data/bin/fOOrth +8 -0
  7. data/demo.rb +24 -0
  8. data/fOOrth.gemspec +40 -0
  9. data/fOOrth.reek +109 -0
  10. data/integration/README.md +12 -0
  11. data/integration/_FILE_test.foorth +5 -0
  12. data/integration/array_lib_tests.rb +360 -0
  13. data/integration/class_lib_tests.rb +116 -0
  14. data/integration/clone_lib_tests.rb +108 -0
  15. data/integration/comparison_tests.rb +132 -0
  16. data/integration/compile_lib_tests.rb +190 -0
  17. data/integration/ctrl_struct_lib_tests.rb +80 -0
  18. data/integration/data_ref_lib_tests.rb +43 -0
  19. data/integration/exception_lib_tests.rb +86 -0
  20. data/integration/fiber_bundle_tests.rb +380 -0
  21. data/integration/hash_lib_tests.rb +120 -0
  22. data/integration/in_stream_test_1.txt +4 -0
  23. data/integration/load_test_one.foorth +6 -0
  24. data/integration/load_test_two.foorth +4 -0
  25. data/integration/numeric_lib_tests.rb +321 -0
  26. data/integration/object_lib_tests.rb +38 -0
  27. data/integration/procedure_lib_tests.rb +40 -0
  28. data/integration/queue_lib_tests.rb +66 -0
  29. data/integration/stack_lib_tests.rb +70 -0
  30. data/integration/standard_lib_tests.rb +208 -0
  31. data/integration/stdio_lib_tests.rb +52 -0
  32. data/integration/stream_lib_tests.rb +196 -0
  33. data/integration/string_lib_tests.rb +217 -0
  34. data/integration/support/foorth_testing.rb +135 -0
  35. data/integration/thread_lib_tests.rb +83 -0
  36. data/integration/time_lib_tests.rb +791 -0
  37. data/integration/vm_lib_tests.rb +38 -0
  38. data/lib/fOOrth.rb +57 -0
  39. data/lib/fOOrth/compiler.rb +78 -0
  40. data/lib/fOOrth/compiler/context.rb +49 -0
  41. data/lib/fOOrth/compiler/context/locals.rb +34 -0
  42. data/lib/fOOrth/compiler/context/map_name.rb +92 -0
  43. data/lib/fOOrth/compiler/context/tags.rb +48 -0
  44. data/lib/fOOrth/compiler/modes.rb +32 -0
  45. data/lib/fOOrth/compiler/modes/compiled.rb +41 -0
  46. data/lib/fOOrth/compiler/modes/deferred.rb +57 -0
  47. data/lib/fOOrth/compiler/modes/delayed.rb +40 -0
  48. data/lib/fOOrth/compiler/modes/nested.rb +34 -0
  49. data/lib/fOOrth/compiler/modes/suspend.rb +32 -0
  50. data/lib/fOOrth/compiler/parser.rb +26 -0
  51. data/lib/fOOrth/compiler/parser/get_string.rb +71 -0
  52. data/lib/fOOrth/compiler/parser/normal.rb +53 -0
  53. data/lib/fOOrth/compiler/parser/skip.rb +50 -0
  54. data/lib/fOOrth/compiler/parser/special.rb +42 -0
  55. data/lib/fOOrth/compiler/process.rb +47 -0
  56. data/lib/fOOrth/compiler/process/generate.rb +24 -0
  57. data/lib/fOOrth/compiler/process/get_token.rb +23 -0
  58. data/lib/fOOrth/compiler/process/procedure.rb +55 -0
  59. data/lib/fOOrth/compiler/process/string.rb +20 -0
  60. data/lib/fOOrth/compiler/source.rb +51 -0
  61. data/lib/fOOrth/compiler/source/console.rb +70 -0
  62. data/lib/fOOrth/compiler/source/file_source.rb +37 -0
  63. data/lib/fOOrth/compiler/source/read_point.rb +46 -0
  64. data/lib/fOOrth/compiler/source/string_source.rb +28 -0
  65. data/lib/fOOrth/compiler/token.rb +37 -0
  66. data/lib/fOOrth/compiler/word_specs.rb +178 -0
  67. data/lib/fOOrth/core.rb +27 -0
  68. data/lib/fOOrth/core/class.rb +116 -0
  69. data/lib/fOOrth/core/object.rb +78 -0
  70. data/lib/fOOrth/core/virtual_machine.rb +28 -0
  71. data/lib/fOOrth/debug.rb +13 -0
  72. data/lib/fOOrth/debug/context_dump.rb +31 -0
  73. data/lib/fOOrth/debug/dbg_puts.rb +17 -0
  74. data/lib/fOOrth/debug/display_abort.rb +37 -0
  75. data/lib/fOOrth/debug/vm_dump.rb +27 -0
  76. data/lib/fOOrth/initialize.rb +83 -0
  77. data/lib/fOOrth/interpreter.rb +24 -0
  78. data/lib/fOOrth/interpreter/add_to_hash.rb +17 -0
  79. data/lib/fOOrth/interpreter/data_stack.rb +125 -0
  80. data/lib/fOOrth/interpreter/do_loop.rb +55 -0
  81. data/lib/fOOrth/interpreter/squash.rb +25 -0
  82. data/lib/fOOrth/library.rb +38 -0
  83. data/lib/fOOrth/library/array_library.rb +577 -0
  84. data/lib/fOOrth/library/bundle_library.rb +112 -0
  85. data/lib/fOOrth/library/class_library.rb +90 -0
  86. data/lib/fOOrth/library/clone_library.rb +72 -0
  87. data/lib/fOOrth/library/command_library.rb +205 -0
  88. data/lib/fOOrth/library/compile_library.rb +181 -0
  89. data/lib/fOOrth/library/complex_library.rb +81 -0
  90. data/lib/fOOrth/library/ctrl_struct_library.rb +116 -0
  91. data/lib/fOOrth/library/data_ref_library.rb +100 -0
  92. data/lib/fOOrth/library/duration/arithmetic.rb +114 -0
  93. data/lib/fOOrth/library/duration/formatter.rb +152 -0
  94. data/lib/fOOrth/library/duration/intervals.rb +233 -0
  95. data/lib/fOOrth/library/duration/make.rb +75 -0
  96. data/lib/fOOrth/library/duration_library.rb +52 -0
  97. data/lib/fOOrth/library/fiber_library.rb +120 -0
  98. data/lib/fOOrth/library/hash_library.rb +203 -0
  99. data/lib/fOOrth/library/in_stream_library.rb +81 -0
  100. data/lib/fOOrth/library/integer_library.rb +104 -0
  101. data/lib/fOOrth/library/mutex_library.rb +31 -0
  102. data/lib/fOOrth/library/numeric_library.rb +380 -0
  103. data/lib/fOOrth/library/object_library.rb +80 -0
  104. data/lib/fOOrth/library/other_value_types_library.rb +96 -0
  105. data/lib/fOOrth/library/out_stream_library.rb +146 -0
  106. data/lib/fOOrth/library/procedure_library.rb +65 -0
  107. data/lib/fOOrth/library/queue_library.rb +47 -0
  108. data/lib/fOOrth/library/rational_library.rb +90 -0
  109. data/lib/fOOrth/library/stack_library.rb +56 -0
  110. data/lib/fOOrth/library/stdio_library.rb +56 -0
  111. data/lib/fOOrth/library/string_library.rb +285 -0
  112. data/lib/fOOrth/library/stubs.rb +76 -0
  113. data/lib/fOOrth/library/sync_bundle_library.rb +50 -0
  114. data/lib/fOOrth/library/thread_library.rb +73 -0
  115. data/lib/fOOrth/library/time_library.rb +302 -0
  116. data/lib/fOOrth/library/vm_library.rb +105 -0
  117. data/lib/fOOrth/main.rb +125 -0
  118. data/lib/fOOrth/monkey_patch.rb +14 -0
  119. data/lib/fOOrth/monkey_patch/complex.rb +30 -0
  120. data/lib/fOOrth/monkey_patch/exceptions.rb +154 -0
  121. data/lib/fOOrth/monkey_patch/false.rb +11 -0
  122. data/lib/fOOrth/monkey_patch/float.rb +22 -0
  123. data/lib/fOOrth/monkey_patch/integer.rb +22 -0
  124. data/lib/fOOrth/monkey_patch/nil.rb +11 -0
  125. data/lib/fOOrth/monkey_patch/numeric.rb +33 -0
  126. data/lib/fOOrth/monkey_patch/object.rb +43 -0
  127. data/lib/fOOrth/monkey_patch/rational.rb +31 -0
  128. data/lib/fOOrth/monkey_patch/string.rb +51 -0
  129. data/lib/fOOrth/symbol_map.rb +82 -0
  130. data/lib/fOOrth/version.rb +7 -0
  131. data/license.txt +21 -0
  132. data/rakefile.rb +65 -0
  133. data/reek.txt +1 -0
  134. data/sire.rb +132 -0
  135. data/t.txt +3 -0
  136. data/test.foorth +5 -0
  137. data/tests/compiler/context_tests.rb +180 -0
  138. data/tests/compiler/file_source_test_one.txt +1 -0
  139. data/tests/compiler/file_source_test_three.txt +3 -0
  140. data/tests/compiler/file_source_test_two.txt +3 -0
  141. data/tests/compiler/file_source_tests.rb +130 -0
  142. data/tests/compiler/mode_tests.rb +45 -0
  143. data/tests/compiler/parser_tests.rb +116 -0
  144. data/tests/compiler/spec_tests.rb +113 -0
  145. data/tests/compiler/string_source_tests.rb +128 -0
  146. data/tests/core_tests.rb +138 -0
  147. data/tests/interpreter/data_stack_tests.rb +119 -0
  148. data/tests/monkey_patch/coerce_test.rb +131 -0
  149. data/tests/monkey_patch/complex_test.rb +25 -0
  150. data/tests/monkey_patch/numeric_test.rb +62 -0
  151. data/tests/monkey_patch/object_test.rb +49 -0
  152. data/tests/monkey_patch/rational_test.rb +57 -0
  153. data/tests/monkey_patch/string_test.rb +53 -0
  154. data/tests/symbol_map_tests.rb +53 -0
  155. metadata +366 -0
@@ -0,0 +1,138 @@
1
+ # coding: utf-8
2
+
3
+ $exclude_fOOrth_library = true
4
+ require_relative '../lib/fOOrth'
5
+ gem 'minitest'
6
+ require 'minitest/autorun'
7
+ require 'minitest_visible'
8
+
9
+ #Test the monkey patches applied to the Object class.
10
+ class CoreTester < Minitest::Test
11
+
12
+ #Track mini-test progress.
13
+ include MinitestVisible
14
+
15
+ def test_that_object_handles_no_method_errors
16
+ #Get an object instance to test with.
17
+ obj = Object.new
18
+
19
+ XfOOrth::SymbolMap.add_entry("a_test_zero", :a_test_zero)
20
+
21
+ assert_raises(XfOOrth::XfOOrthError) do
22
+ obj.a_test_zero
23
+ end
24
+
25
+ assert_raises(NoMethodError) do
26
+ obj.qwertyuiop
27
+ end
28
+ end
29
+
30
+ def test_that_shared_methods_can_be_defined
31
+ #Get an object instance to test with.
32
+ obj = Object.new
33
+
34
+ #Get the virtual machine.
35
+ vm = Thread.current[:vm]
36
+
37
+ XfOOrth::SymbolMap.add_entry("a_test_one", :a_test_one)
38
+
39
+ spec = Object.create_shared_method("a_test_one", XfOOrth::TosSpec, []) {|vm| vm.push(9671111) }
40
+
41
+ obj.a_test_one(vm)
42
+
43
+ assert_equal(9671111, vm.pop)
44
+ assert_equal(spec, Object.map_foorth_shared(:a_test_one))
45
+ assert_equal(spec, Class.map_foorth_shared(:a_test_one))
46
+ assert_equal(spec, Object.foorth_shared[:a_test_one])
47
+ assert_equal(nil, Class.foorth_shared[:a_test_one])
48
+ end
49
+
50
+ def test_that_exclusive_methods_can_be_defined
51
+ #Get an object instance to test with.
52
+ obj = Object.new
53
+
54
+ #Get the virtual machine.
55
+ vm = XfOOrth::VirtualMachine.vm
56
+
57
+ XfOOrth::SymbolMap.add_entry("a_test_two", :a_test_two)
58
+
59
+ spec = obj.create_exclusive_method("a_test_two", XfOOrth::TosSpec, []) do |vm|
60
+ vm.push(9686668)
61
+ end
62
+
63
+ obj.a_test_two(vm)
64
+
65
+ assert_equal(9686668, vm.pop)
66
+ assert_equal(spec, obj.map_foorth_exclusive(:a_test_two))
67
+ assert_equal(spec, obj.foorth_exclusive[:a_test_two])
68
+ assert_equal(nil, Object.map_foorth_shared(:a_test_two))
69
+ assert_equal(nil, Class.map_foorth_shared(:a_test_two))
70
+ end
71
+
72
+ def test_class_naming
73
+ assert_equal("Object", Object.foorth_name)
74
+ assert_equal("Class", Class.foorth_name)
75
+ assert_equal("VirtualMachine", XfOOrth::VirtualMachine.foorth_name)
76
+
77
+ vm = Thread.current[:vm]
78
+ assert_equal("VirtualMachine instance <Main>", vm.foorth_name)
79
+ end
80
+
81
+ def test_instance_naming
82
+ obj = Object.new
83
+ assert_equal("Object instance", obj.foorth_name)
84
+
85
+ vm = Thread.current[:vm]
86
+ assert_equal("VirtualMachine instance <Main>", vm.foorth_name)
87
+ end
88
+
89
+ def test_that_virtual_machine_rejects_new
90
+ assert_raises(XfOOrth::XfOOrthError) do
91
+ XfOOrth::VirtualMachine.new('Fails')
92
+ end
93
+ end
94
+
95
+ def test_that_the_VM_class_does_not_subclass
96
+ assert_raises(XfOOrth::XfOOrthError) do
97
+ XfOOrth::VirtualMachine.create_foorth_subclass("MyClass")
98
+ end
99
+ end
100
+
101
+ def test_creating_subclasses
102
+ new_class = Object.create_foorth_subclass('MyClass')
103
+ symbol = XfOOrth::SymbolMap.map('MyClass')
104
+
105
+ assert($FOORTH_GLOBALS[symbol])
106
+ assert_equal('XfOOrth::ClassSpec instance', new_class.foorth_name)
107
+ assert_equal(XfOOrth::XfOOrth_MyClass, new_class.new_class)
108
+ assert_equal(XfOOrth::XfOOrth_MyClass, $FOORTH_GLOBALS[symbol].new_class)
109
+
110
+ assert_raises(XfOOrth::XfOOrthError) do
111
+ no_class = Object.create_foorth_subclass('No Class')
112
+ end
113
+
114
+ assert_raises(XfOOrth::XfOOrthError) do
115
+ copy_class = Object.create_foorth_subclass('MyClass')
116
+ end
117
+ end
118
+
119
+ def test_creating_proxies
120
+ new_proxy = String.create_foorth_proxy
121
+ symbol = XfOOrth::SymbolMap.map('String')
122
+
123
+ assert($FOORTH_GLOBALS[symbol])
124
+ assert_equal('String', new_proxy.new_class.foorth_name)
125
+ assert_equal(String, new_proxy.new_class)
126
+ assert_equal(String, $FOORTH_GLOBALS[symbol].new_class)
127
+
128
+ assert_raises(XfOOrth::XfOOrthError) do
129
+ bad_class = Object.create_foorth_subclass('My Class')
130
+ end
131
+
132
+ assert_raises(XfOOrth::XfOOrthError) do
133
+ bad_class = Module.create_foorth_proxy('Mod ule')
134
+ end
135
+
136
+ end
137
+
138
+ end
@@ -0,0 +1,119 @@
1
+ # coding: utf-8
2
+
3
+ $exclude_fOOrth_library = true
4
+ require_relative '../../lib/fOOrth'
5
+ gem 'minitest'
6
+ require 'minitest/autorun'
7
+ require 'minitest_visible'
8
+
9
+ #Test the monkey patches applied to the Object class.
10
+ class DataStackMapTester < Minitest::Test
11
+
12
+ #Track mini-test progress.
13
+ include MinitestVisible
14
+
15
+ def test_data_stack_ops
16
+ vm = Thread.current[:vm]
17
+ refute(vm == nil)
18
+
19
+ vm.interpreter_reset
20
+
21
+ assert_equal(vm.data_stack, [])
22
+ vm.push(4)
23
+ assert_equal(vm.data_stack, [4])
24
+ vm.push(8)
25
+ assert_equal(vm.data_stack, [4, 8])
26
+ assert_equal(vm.peek, 8)
27
+ assert_equal(vm.peek(1), 8)
28
+ assert_equal(vm.peek(2), 4)
29
+ assert_equal(vm.pop, 8)
30
+ assert_equal(vm.data_stack, [4])
31
+ assert_equal(vm.pop, 4)
32
+ assert_equal(vm.data_stack, [])
33
+
34
+ vm.push(8)
35
+ assert_equal(vm.data_stack, [8])
36
+ vm.pushm([1,2,3])
37
+ assert_equal(vm.data_stack, [8, 1, 2, 3])
38
+
39
+ assert_raises(XfOOrth::XfOOrthError) do
40
+ vm.peek(-1)
41
+ end
42
+
43
+ assert_raises(XfOOrth::XfOOrthError) do
44
+ vm.peek(0)
45
+ end
46
+ end
47
+
48
+ def test_boolean_stack_data
49
+ vm = Thread.current[:vm]
50
+ refute(vm == nil)
51
+
52
+ vm.interpreter_reset
53
+
54
+ vm.push(16)
55
+ assert(vm.pop?)
56
+
57
+ vm.push(true)
58
+ assert(vm.pop?)
59
+
60
+ vm.push('test')
61
+ assert(vm.pop?)
62
+
63
+ vm.push(0)
64
+ assert(vm.pop?)
65
+
66
+ vm.push(false)
67
+ refute(vm.pop?)
68
+
69
+ vm.push('')
70
+ assert(vm.pop?)
71
+
72
+ vm.push(nil)
73
+ refute(vm.pop?)
74
+ end
75
+
76
+ def test_pop_multiple
77
+ vm = Thread.current[:vm]
78
+ refute(vm == nil)
79
+
80
+ vm.interpreter_reset
81
+
82
+ vm.push(16)
83
+ vm.push(true)
84
+ vm.push('test')
85
+ vm.push(0)
86
+ vm.push('')
87
+ vm.push(nil)
88
+
89
+ assert_equal(vm.data_stack, [16, true, 'test', 0, '', nil])
90
+ assert_equal(vm.popm(3), [0, '', nil])
91
+ assert_equal(vm.popm(3), [16, true, 'test'])
92
+ assert_equal(vm.data_stack, [])
93
+ end
94
+
95
+ def test_poke
96
+ vm = Thread.current[:vm]
97
+ refute(vm == nil)
98
+
99
+ vm.interpreter_reset
100
+
101
+ vm.push(42)
102
+ vm.poke("hello")
103
+ assert_equal(vm.data_stack, ["hello"])
104
+ end
105
+
106
+ def test_swap_pop
107
+ vm = Thread.current[:vm]
108
+ refute(vm == nil)
109
+
110
+ vm.interpreter_reset
111
+
112
+ vm.push(16)
113
+ vm.push(99)
114
+ assert_equal(vm.swap_pop, 16)
115
+ assert_equal(vm.pop, 99)
116
+ assert_equal(vm.data_stack, [])
117
+ end
118
+
119
+ end
@@ -0,0 +1,131 @@
1
+ # coding: utf-8
2
+
3
+ require_relative '../../lib/fOOrth/monkey_patch'
4
+ gem 'minitest'
5
+ require 'minitest/autorun'
6
+ require 'minitest_visible'
7
+
8
+ #Test the monkey patches applied to the coerce protocol.
9
+ class CoerceProtocolTester < Minitest::Test
10
+
11
+ #Track mini-test progress.
12
+ include MinitestVisible
13
+
14
+ #Test the default stubs.
15
+ def test_object_stubs
16
+ obj = Object.new
17
+
18
+ assert_raises(XfOOrth::XfOOrthError) { obj.foorth_coerce(42) }
19
+ end
20
+
21
+ def test_coerce_for_integers
22
+ obj = Object.new
23
+
24
+ assert_equal(42, (3).foorth_coerce(42))
25
+ assert_equal(Fixnum, (3).foorth_coerce(42).class)
26
+
27
+ assert_equal(42, (3).foorth_coerce('42'))
28
+ assert_equal(Fixnum, (3).foorth_coerce('42').class)
29
+
30
+ assert_raises(XfOOrth::XfOOrthError) { (3).foorth_coerce('turnip') }
31
+ assert_raises(XfOOrth::XfOOrthError) { (3).foorth_coerce(obj) }
32
+ assert_raises(XfOOrth::XfOOrthError) { (3).foorth_coerce(nil) }
33
+
34
+ assert_equal(42, Integer.foorth_coerce(42))
35
+ assert_equal(Fixnum, Integer.foorth_coerce(42).class)
36
+
37
+ assert_equal(42, Integer.foorth_coerce('42'))
38
+ assert_equal(Fixnum, Integer.foorth_coerce('42').class)
39
+
40
+ assert_raises(XfOOrth::XfOOrthError) { Integer.foorth_coerce('turnip') }
41
+ assert_raises(XfOOrth::XfOOrthError) { Integer.foorth_coerce(obj) }
42
+ assert_raises(XfOOrth::XfOOrthError) { Integer.foorth_coerce(nil) }
43
+
44
+ end
45
+
46
+ def test_coerce_for_floats
47
+ obj = Object.new
48
+
49
+ assert_equal(42.0, (3.0).foorth_coerce(42))
50
+ assert_equal(Float, (3.0).foorth_coerce(42).class)
51
+
52
+ assert_equal(42.0, (3.0).foorth_coerce(42.0))
53
+ assert_equal(Float, (3.0).foorth_coerce(42.0).class)
54
+
55
+ assert_equal(42.0, (3.0).foorth_coerce('42'))
56
+ assert_equal(Float, (3.0).foorth_coerce('42').class)
57
+
58
+ assert_equal(42.0, (3.0).foorth_coerce('42.0'))
59
+ assert_equal(Float, (3.0).foorth_coerce('42.0').class)
60
+
61
+ assert_raises(XfOOrth::XfOOrthError) { (3.0).foorth_coerce('turnip') }
62
+ assert_raises(XfOOrth::XfOOrthError) { (3.0).foorth_coerce(obj) }
63
+ assert_raises(XfOOrth::XfOOrthError) { (3.0).foorth_coerce(nil) }
64
+
65
+ assert_equal(42.0, Float.foorth_coerce(42))
66
+ assert_equal(Float, Float.foorth_coerce(42).class)
67
+
68
+ assert_equal(42.0, Float.foorth_coerce(42.0))
69
+ assert_equal(Float, Float.foorth_coerce(42.0).class)
70
+
71
+ assert_equal(42.0, Float.foorth_coerce('42'))
72
+ assert_equal(Float, Float.foorth_coerce('42').class)
73
+
74
+ assert_equal(42.0, Float.foorth_coerce('42.0'))
75
+ assert_equal(Float, Float.foorth_coerce('42.0').class)
76
+
77
+ assert_raises(XfOOrth::XfOOrthError) { Float.foorth_coerce('turnip') }
78
+ assert_raises(XfOOrth::XfOOrthError) { Float.foorth_coerce(obj) }
79
+ assert_raises(XfOOrth::XfOOrthError) { Float.foorth_coerce(nil) }
80
+ end
81
+
82
+ def test_coerce_for_rationals
83
+ obj = Object.new
84
+
85
+ assert_equal('42/1'.to_r, ('3/1'.to_r).foorth_coerce(42))
86
+ assert_equal(Rational, ('3/1'.to_r).foorth_coerce(42).class)
87
+
88
+ assert_equal('42/1'.to_r, ('3/1'.to_r).foorth_coerce(42.0))
89
+ assert_equal(Rational, ('3/1'.to_r).foorth_coerce(42.0).class)
90
+
91
+ assert_equal('42/1'.to_r, ('3/1'.to_r).foorth_coerce('42'))
92
+ assert_equal(Rational, ('3/1'.to_r).foorth_coerce('42').class)
93
+
94
+ assert_equal('42/1'.to_r, ('3/1'.to_r).foorth_coerce('42.0'))
95
+ assert_equal(Rational, ('3/1'.to_r).foorth_coerce('42.0').class)
96
+
97
+ assert_equal('42/1'.to_r, ('3/1'.to_r).foorth_coerce('42/1'))
98
+ assert_equal(Rational, ('3/1'.to_r).foorth_coerce('42/1').class)
99
+
100
+ assert_raises(XfOOrth::XfOOrthError) { ('3/1'.to_r).foorth_coerce('turnip') }
101
+ assert_raises(XfOOrth::XfOOrthError) { ('3/1'.to_r).foorth_coerce(obj) }
102
+ assert_raises(XfOOrth::XfOOrthError) { ('3/1'.to_r).foorth_coerce(nil) }
103
+ end
104
+
105
+ def test_coerce_for_complex
106
+ obj = Object.new
107
+
108
+ assert_equal('42+0i'.to_c, ('1+1i'.to_c).foorth_coerce(42))
109
+ assert_equal(Complex, ('1+1i'.to_c).foorth_coerce(42).class)
110
+
111
+ assert_equal('42+0i'.to_c, ('1+1i'.to_c).foorth_coerce(42.0))
112
+ assert_equal(Complex, ('1+1i'.to_c).foorth_coerce(42.0).class)
113
+
114
+ assert_equal('42+0i'.to_c, ('1+1i'.to_c).foorth_coerce('42/1'.to_r))
115
+ assert_equal(Complex, ('1+1i'.to_c).foorth_coerce('42/1'.to_r).class)
116
+
117
+ assert_equal('42+0i'.to_c, ('1+1i'.to_c).foorth_coerce('42'))
118
+ assert_equal(Complex, ('1+1i'.to_c).foorth_coerce('42').class)
119
+
120
+ assert_equal('42+0i'.to_c, ('1+1i'.to_c).foorth_coerce('42.0'))
121
+ assert_equal(Complex, ('1+1i'.to_c).foorth_coerce('42.0').class)
122
+
123
+ assert_equal('42+0i'.to_c, ('1+1i'.to_c).foorth_coerce('42/1'))
124
+ assert_equal(Complex, ('1+1i'.to_c).foorth_coerce('42/1').class)
125
+
126
+ assert_raises(XfOOrth::XfOOrthError) { ('1+1i'.to_c).foorth_coerce('turnip') }
127
+ assert_raises(XfOOrth::XfOOrthError) { ('1+1i'.to_c).foorth_coerce(obj) }
128
+ assert_raises(XfOOrth::XfOOrthError) { ('1+1i'.to_c).foorth_coerce(nil) }
129
+ end
130
+
131
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+
3
+ require_relative '../../lib/fOOrth/monkey_patch/numeric'
4
+ require_relative '../../lib/fOOrth/monkey_patch/complex'
5
+ gem 'minitest'
6
+ require 'minitest/autorun'
7
+ require 'minitest_visible'
8
+
9
+ #Test the monkey patches applied to the Object class.
10
+ class ComplexMonkeyPatchTester < Minitest::Test
11
+
12
+ #Track mini-test progress.
13
+ include MinitestVisible
14
+
15
+ #Test that it embeds
16
+ def test_foorth_embed
17
+ comp = Complex(1,2)
18
+ assert_equal(comp.foorth_embed, 'Complex(1,2)')
19
+ end
20
+
21
+ def test_that_it_is_not_rationalizable
22
+ comp = Complex(1,2)
23
+ assert_equal(nil, comp.to_foorth_r)
24
+ end
25
+ end
@@ -0,0 +1,62 @@
1
+ # coding: utf-8
2
+
3
+ require_relative '../../lib/fOOrth/monkey_patch/numeric'
4
+ gem 'minitest'
5
+ require 'minitest/autorun'
6
+ require 'minitest_visible'
7
+
8
+ #Test the monkey patches applied to the Object class.
9
+ class NumericMonkeyPatchTester < Minitest::Test
10
+
11
+ #Track mini-test progress.
12
+ include MinitestVisible
13
+
14
+ #Test that it embeds
15
+ def test_foorth_embed
16
+ assert_equal(5.foorth_embed, '5')
17
+ assert_equal((5.1).foorth_embed, '5.1')
18
+ end
19
+
20
+ #Test for conversion to a character.
21
+ def test_to_character
22
+ assert_equal(65.to_foorth_c, 'A')
23
+ assert_equal((65.0).to_foorth_c, 'A')
24
+ assert_equal(Rational(65,1).to_foorth_c, 'A')
25
+
26
+ assert_equal(127.to_foorth_c, "\u007F")
27
+ assert_equal(128.to_foorth_c, "\u0080")
28
+
29
+ assert_equal(255.to_foorth_c, "\u00FF")
30
+ assert_equal(256.to_foorth_c, "\u0100")
31
+
32
+ assert_equal(169.to_foorth_c, "\u00A9")
33
+ assert_equal(1120.to_foorth_c, "\u0460")
34
+
35
+ assert_raises(XfOOrth::XfOOrthError) do
36
+ Complex(65,0).to_foorth_c
37
+ end
38
+
39
+ assert_raises(XfOOrth::XfOOrthError) do
40
+ 1114112.to_foorth_c
41
+ end
42
+
43
+ assert_raises(XfOOrth::XfOOrthError) do
44
+ (-2).to_foorth_c
45
+ end
46
+ end
47
+
48
+ #Test for conversion to a numeric.
49
+ def test_to_number
50
+ assert_equal(65.to_foorth_n, 65)
51
+ assert_equal((65.1).to_foorth_n, 65.1)
52
+ assert_equal(Complex(65,0).to_foorth_n, Complex(65,0))
53
+ assert_equal(Rational(65,1).to_foorth_n, Rational(65,1))
54
+ end
55
+
56
+ #Test for conversion to a rational.
57
+ def test_to_rational
58
+ assert_equal(Rational(13,10), (1.3).to_foorth_r)
59
+ end
60
+
61
+ end
62
+