fOOrth 0.6.6 → 0.6.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (67) hide show
  1. checksums.yaml +5 -5
  2. data/CODE_OF_CONDUCT.md +49 -0
  3. data/README.md +32 -1
  4. data/fOOrth.gemspec +3 -3
  5. data/integration/array_lib_tests.rb +10 -0
  6. data/integration/compile_lib_tests.rb +67 -1
  7. data/integration/exception_lib_tests.rb +4 -0
  8. data/integration/hash_lib_tests.rb +9 -0
  9. data/integration/numeric_lib_tests.rb +326 -321
  10. data/integration/procedure_lib_tests.rb +16 -0
  11. data/integration/queue_lib_tests.rb +2 -1
  12. data/integration/stack_lib_tests.rb +2 -1
  13. data/integration/stdio_lib_tests.rb +62 -0
  14. data/integration/string_lib_tests.rb +11 -0
  15. data/integration/thread_lib_tests.rb +19 -5
  16. data/lib/fOOrth.rb +0 -2
  17. data/lib/fOOrth/compiler/context.rb +64 -64
  18. data/lib/fOOrth/compiler/context/locals.rb +34 -34
  19. data/lib/fOOrth/compiler/context/map_name.rb +85 -74
  20. data/lib/fOOrth/compiler/context/tags.rb +60 -48
  21. data/lib/fOOrth/compiler/process/generate.rb +1 -1
  22. data/lib/fOOrth/compiler/process/procedure.rb +40 -0
  23. data/lib/fOOrth/compiler/word_specs.rb +3 -3
  24. data/lib/fOOrth/core/object.rb +1 -1
  25. data/lib/fOOrth/library.rb +3 -0
  26. data/lib/fOOrth/library/alias_library.rb +126 -0
  27. data/lib/fOOrth/library/array_library.rb +41 -21
  28. data/lib/fOOrth/library/command_library.rb +1 -1
  29. data/lib/fOOrth/library/compile_library.rb +266 -264
  30. data/lib/fOOrth/library/complex_library.rb +82 -80
  31. data/lib/fOOrth/library/float_library.rb +37 -0
  32. data/lib/fOOrth/library/formatting/array.rb +90 -0
  33. data/lib/fOOrth/library/formatting/bullets.rb +15 -79
  34. data/lib/fOOrth/library/formatting/columns.rb +20 -42
  35. data/lib/fOOrth/library/formatting/hash.rb +29 -0
  36. data/lib/fOOrth/library/formatting/nil.rb +13 -0
  37. data/lib/fOOrth/library/formatting/object.rb +18 -0
  38. data/lib/fOOrth/library/formatting/string.rb +46 -0
  39. data/lib/fOOrth/library/hash_library.rb +14 -6
  40. data/lib/fOOrth/library/introspection/class.rb +20 -18
  41. data/lib/fOOrth/library/introspection/context.rb +3 -2
  42. data/lib/fOOrth/library/introspection/object.rb +42 -20
  43. data/lib/fOOrth/library/introspection/string.rb +21 -5
  44. data/lib/fOOrth/library/introspection/vm.rb +17 -29
  45. data/lib/fOOrth/library/mutex_library.rb +8 -1
  46. data/lib/fOOrth/library/numeric_library.rb +359 -380
  47. data/lib/fOOrth/library/procedure_library.rb +69 -65
  48. data/lib/fOOrth/library/queue_library.rb +6 -1
  49. data/lib/fOOrth/library/rational_library.rb +89 -89
  50. data/lib/fOOrth/library/stack_library.rb +6 -1
  51. data/lib/fOOrth/library/stdio_library.rb +11 -8
  52. data/lib/fOOrth/library/string_library.rb +21 -6
  53. data/lib/fOOrth/library/stubs_library.rb +49 -0
  54. data/lib/fOOrth/monkey_patch/exceptions.rb +2 -6
  55. data/lib/fOOrth/monkey_patch/object.rb +7 -0
  56. data/lib/fOOrth/version.rb +1 -1
  57. data/reek.txt +1 -59
  58. data/sire.rb +0 -1
  59. data/tests/compiler/context_tests.rb +188 -177
  60. data/tests/compiler/file_source_tests.rb +130 -130
  61. data/tests/compiler/parser_tests.rb +4 -4
  62. data/tests/compiler/string_source_tests.rb +4 -4
  63. data/tests/core_tests.rb +138 -138
  64. data/tests/monkey_patch/complex_test.rb +24 -24
  65. data/tests/monkey_patch/object_test.rb +49 -49
  66. data/tests/monkey_patch/string_test.rb +61 -61
  67. metadata +20 -13
@@ -0,0 +1,49 @@
1
+ # coding: utf-8
2
+
3
+ #* library/stubs_library.rb - Support for method stubs in fOOrth.
4
+ module XfOOrth
5
+ VirtualMachine.create_shared_method('stub:', VmSpec, [:immediate],
6
+ &lambda {|vm|
7
+ name = parser.get_word().inspect
8
+ process_text("vm.class.create_shared_method(#{name}, VmSpec, [:stub]); ")
9
+ })
10
+
11
+ VirtualMachine.create_shared_method('.stub:', VmSpec, [:immediate],
12
+ &lambda {|vm|
13
+ name = parser.get_word().inspect
14
+ process_text("vm.create_shared_stub(#{name}); ")
15
+ })
16
+
17
+ VirtualMachine.create_shared_method('.stub::', VmSpec, [:immediate],
18
+ &lambda {|vm|
19
+ name = parser.get_word().inspect
20
+ process_text("vm.create_exclusive_stub(#{name}); ")
21
+ })
22
+
23
+ # Stub support methods in the VirtualMachine class.
24
+ class VirtualMachine
25
+
26
+ #Create a shared method stub
27
+ def create_shared_stub(name)
28
+ unless (target = pop).is_a?(Class)
29
+ error "F13: The target of .stub: must be a class"
30
+ end
31
+
32
+ type = XfOOrth.name_to_type(name, self)
33
+ XfOOrth.validate_type(self, type, name)
34
+ target.create_shared_method(name, type, [:stub])
35
+ clear_cast
36
+ end
37
+
38
+ #Create an exclusive method stub
39
+ def create_exclusive_stub(name)
40
+ target = pop
41
+ type = XfOOrth.name_to_type(name, self)
42
+ XfOOrth.validate_type(self, type, name)
43
+ target.create_exclusive_method(name, type, [:stub])
44
+ clear_cast
45
+ end
46
+
47
+ end
48
+
49
+ end
@@ -108,13 +108,9 @@ end
108
108
  #Extensions to the RunTimeError to support fOOrth
109
109
  class RuntimeError
110
110
 
111
- #Massage the messages a bit
112
- #<br>Endemic Code Smells
113
- #* :reek:FeatureEnvy
111
+ #Massage the messages a bit.
114
112
  def message
115
- msg = super
116
- msg["frozen"] && msg["frozen"] = "protected"
117
- msg
113
+ super.gsub(/frozen/, "protected")
118
114
  end
119
115
 
120
116
  end
@@ -38,6 +38,13 @@ class Object
38
38
  fail XfOOrth::XfOOrthError, msg, caller
39
39
  end
40
40
 
41
+ #A helper macro for method not understood errors.
42
+ def f20_error(recvr, name, symbol)
43
+ fail XfOOrth::XfOOrthError,
44
+ "F20: A #{recvr.foorth_name} does not understand #{name} (#{symbol.inspect}).",
45
+ caller
46
+ end
47
+
41
48
  #Argument coercion methods. These are stubs.
42
49
 
43
50
  #Coerce the argument to match my type. Stub
@@ -3,5 +3,5 @@
3
3
  #* version.rb - The version string for fOOrth.
4
4
  module XfOOrth
5
5
  #The version string for fOOrth.
6
- VERSION = "0.6.6"
6
+ VERSION = "0.6.11"
7
7
  end
data/reek.txt CHANGED
@@ -1,59 +1 @@
1
- lib/fOOrth/compiler/context/map_name.rb -- 1 warning:
2
- [31]:ControlParameter: XfOOrth::Context#do_map_name is controlled by argument allow_defaults [https://github.com/troessner/reek/blob/master/docs/Control-Parameter.md]
3
- lib/fOOrth/library/compile_library.rb -- 2 warnings:
4
- [230]:ControlParameter: XfOOrth#self.name_to_type is controlled by argument cast_spec [https://github.com/troessner/reek/blob/master/docs/Control-Parameter.md]
5
- [191]:TooManyStatements: XfOOrth#self.add_common_compiler_locals has approx 8 statements [https://github.com/troessner/reek/blob/master/docs/Too-Many-Statements.md]
6
- lib/fOOrth/library/formatting/bullets.rb -- 10 warnings:
7
- [20, 22]:DuplicateMethodCall: XfOOrth::BulletPoints#add calls raw_bullet.in_array 2 times [https://github.com/troessner/reek/blob/master/docs/Duplicate-Method-Call.md]
8
- [62, 68]:DuplicateMethodCall: XfOOrth::BulletPoints#render_bullet calls ' ' * @key_length 2 times [https://github.com/troessner/reek/blob/master/docs/Duplicate-Method-Call.md]
9
- [67, 76]:DuplicateMethodCall: XfOOrth::BulletPoints#render_bullet calls result << temp 2 times [https://github.com/troessner/reek/blob/master/docs/Duplicate-Method-Call.md]
10
- [63, 69]:DuplicateMethodCall: XfOOrth::BulletPoints#render_bullet calls temp.length 2 times [https://github.com/troessner/reek/blob/master/docs/Duplicate-Method-Call.md]
11
- [26, 27]:FeatureEnvy: XfOOrth::BulletPoints#add refers to items more than self (maybe move it to another class?) [https://github.com/troessner/reek/blob/master/docs/Feature-Envy.md]
12
- [20, 22]:FeatureEnvy: XfOOrth::BulletPoints#add refers to raw_bullet more than self (maybe move it to another class?) [https://github.com/troessner/reek/blob/master/docs/Feature-Envy.md]
13
- [18, 23]:FeatureEnvy: XfOOrth::BulletPoints#add refers to raw_item more than self (maybe move it to another class?) [https://github.com/troessner/reek/blob/master/docs/Feature-Envy.md]
14
- [83]:IrresponsibleModule: Array has no descriptive comment [https://github.com/troessner/reek/blob/master/docs/Irresponsible-Module.md]
15
- [103]:IrresponsibleModule: Hash has no descriptive comment [https://github.com/troessner/reek/blob/master/docs/Irresponsible-Module.md]
16
- [51]:TooManyStatements: XfOOrth::BulletPoints#render_bullet has approx 15 statements [https://github.com/troessner/reek/blob/master/docs/Too-Many-Statements.md]
17
- lib/fOOrth/library/formatting/columns.rb -- 2 warnings:
18
- [36]:NestedIterators: XfOOrth::ColumnizedPage#render contains iterators nested 2 deep [https://github.com/troessner/reek/blob/master/docs/Nested-Iterators.md]
19
- [31]:TooManyStatements: XfOOrth::ColumnizedPage#render has approx 8 statements [https://github.com/troessner/reek/blob/master/docs/Too-Many-Statements.md]
20
- lib/fOOrth/library/introspection/class.rb -- 15 warnings:
21
- [35, 42]:DuplicateMethodCall: Class#foorth_method_info calls !spec.has_tag?(:stub) 2 times [https://github.com/troessner/reek/blob/master/docs/Duplicate-Method-Call.md]
22
- [36, 43]:DuplicateMethodCall: Class#foorth_method_info calls (results << ["", ""]).concat(info) 2 times [https://github.com/troessner/reek/blob/master/docs/Duplicate-Method-Call.md]
23
- [36, 43]:DuplicateMethodCall: Class#foorth_method_info calls (results << ["", ""]).concat(info).concat(spec.get_info) 2 times [https://github.com/troessner/reek/blob/master/docs/Duplicate-Method-Call.md]
24
- [36, 43]:DuplicateMethodCall: Class#foorth_method_info calls results << ["", ""] 2 times [https://github.com/troessner/reek/blob/master/docs/Duplicate-Method-Call.md]
25
- [36, 43]:DuplicateMethodCall: Class#foorth_method_info calls spec.get_info 2 times [https://github.com/troessner/reek/blob/master/docs/Duplicate-Method-Call.md]
26
- [35, 42]:DuplicateMethodCall: Class#foorth_method_info calls spec.has_tag?(:stub) 2 times [https://github.com/troessner/reek/blob/master/docs/Duplicate-Method-Call.md]
27
- [62, 74]:DuplicateMethodCall: Class#get_info calls (results << ["", ""]).concat(info) 2 times [https://github.com/troessner/reek/blob/master/docs/Duplicate-Method-Call.md]
28
- [61, 73]:DuplicateMethodCall: Class#get_info calls XfOOrth::SymbolMap.map_info(name) 2 times [https://github.com/troessner/reek/blob/master/docs/Duplicate-Method-Call.md]
29
- [62, 74]:DuplicateMethodCall: Class#get_info calls results << ["", ""] 2 times [https://github.com/troessner/reek/blob/master/docs/Duplicate-Method-Call.md]
30
- [65, 77]:DuplicateMethodCall: Class#get_info calls results.concat(info) 2 times [https://github.com/troessner/reek/blob/master/docs/Duplicate-Method-Call.md]
31
- [65, 77]:DuplicateMethodCall: Class#get_info calls results.concat(info).concat(spec.get_info) 2 times [https://github.com/troessner/reek/blob/master/docs/Duplicate-Method-Call.md]
32
- [65, 77]:DuplicateMethodCall: Class#get_info calls spec.get_info 2 times [https://github.com/troessner/reek/blob/master/docs/Duplicate-Method-Call.md]
33
- [35, 36, 42, 43]:FeatureEnvy: Class#foorth_method_info refers to spec more than self (maybe move it to another class?) [https://github.com/troessner/reek/blob/master/docs/Feature-Envy.md]
34
- [28]:TooManyStatements: Class#foorth_method_info has approx 10 statements [https://github.com/troessner/reek/blob/master/docs/Too-Many-Statements.md]
35
- [54]:TooManyStatements: Class#get_info has approx 14 statements [https://github.com/troessner/reek/blob/master/docs/Too-Many-Statements.md]
36
- lib/fOOrth/library/introspection/context.rb -- 3 warnings:
37
- [14, 28]:DuplicateMethodCall: XfOOrth::Context#get_info calls results << ["", ""] 2 times [https://github.com/troessner/reek/blob/master/docs/Duplicate-Method-Call.md]
38
- [14, 17, 18, 19, 21, 22, 28, 29]:FeatureEnvy: XfOOrth::Context#get_info refers to results more than self (maybe move it to another class?) [https://github.com/troessner/reek/blob/master/docs/Feature-Envy.md]
39
- [10]:TooManyStatements: XfOOrth::Context#get_info has approx 11 statements [https://github.com/troessner/reek/blob/master/docs/Too-Many-Statements.md]
40
- lib/fOOrth/library/introspection/object.rb -- 3 warnings:
41
- [53, 57]:FeatureEnvy: Object#foorth_method_info refers to results more than self (maybe move it to another class?) [https://github.com/troessner/reek/blob/master/docs/Feature-Envy.md]
42
- [52, 53]:FeatureEnvy: Object#foorth_method_info refers to spec more than self (maybe move it to another class?) [https://github.com/troessner/reek/blob/master/docs/Feature-Envy.md]
43
- [18]:TooManyStatements: Object#get_info has approx 12 statements [https://github.com/troessner/reek/blob/master/docs/Too-Many-Statements.md]
44
- lib/fOOrth/library/introspection/string.rb -- 10 warnings:
45
- [17, 20]:DuplicateMethodCall: String#foorth_method_scan calls (results << ["", ""]).concat(info) 2 times [https://github.com/troessner/reek/blob/master/docs/Duplicate-Method-Call.md]
46
- [17, 20]:DuplicateMethodCall: String#foorth_method_scan calls (results << ["", ""]).concat(info).concat(spec.get_info) 2 times [https://github.com/troessner/reek/blob/master/docs/Duplicate-Method-Call.md]
47
- [17, 20]:DuplicateMethodCall: String#foorth_method_scan calls results << ["", ""] 2 times [https://github.com/troessner/reek/blob/master/docs/Duplicate-Method-Call.md]
48
- [17, 20]:DuplicateMethodCall: String#foorth_method_scan calls spec.get_info 2 times [https://github.com/troessner/reek/blob/master/docs/Duplicate-Method-Call.md]
49
- [17, 20, 23]:FeatureEnvy: String#foorth_method_scan refers to found more than self (maybe move it to another class?) [https://github.com/troessner/reek/blob/master/docs/Feature-Envy.md]
50
- [17, 20, 23]:FeatureEnvy: String#foorth_method_scan refers to results more than self (maybe move it to another class?) [https://github.com/troessner/reek/blob/master/docs/Feature-Envy.md]
51
- [13, 17, 20]:FeatureEnvy: String#foorth_method_scan refers to spec more than self (maybe move it to another class?) [https://github.com/troessner/reek/blob/master/docs/Feature-Envy.md]
52
- [7]:TooManyStatements: String#foorth_method_scan has approx 12 statements [https://github.com/troessner/reek/blob/master/docs/Too-Many-Statements.md]
53
- [14]:UncommunicativeVariableName: String#foorth_method_scan has the variable name 'a' [https://github.com/troessner/reek/blob/master/docs/Uncommunicative-Variable-Name.md]
54
- [14]:UncommunicativeVariableName: String#foorth_method_scan has the variable name 'b' [https://github.com/troessner/reek/blob/master/docs/Uncommunicative-Variable-Name.md]
55
- lib/fOOrth/library/introspection/vm.rb -- 3 warnings:
56
- [10]:TooManyStatements: XfOOrth::VirtualMachine#get_info has approx 22 statements [https://github.com/troessner/reek/blob/master/docs/Too-Many-Statements.md]
57
- [47]:UncommunicativeVariableName: XfOOrth::VirtualMachine#get_info has the variable name 'a' [https://github.com/troessner/reek/blob/master/docs/Uncommunicative-Variable-Name.md]
58
- [47]:UncommunicativeVariableName: XfOOrth::VirtualMachine#get_info has the variable name 'b' [https://github.com/troessner/reek/blob/master/docs/Uncommunicative-Variable-Name.md]
59
- 49 total warnings
1
+ 0 total warnings
data/sire.rb CHANGED
@@ -1,7 +1,6 @@
1
1
  # coding: utf-8
2
2
  # A Simple Interactive Ruby Environment
3
3
 
4
- $no_alias_read_line_module = true
5
4
  require 'mini_readline'
6
5
  require 'pp'
7
6
 
@@ -1,177 +1,188 @@
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
- class MockClass
10
- def initialize; @data = {}; end
11
- def []=(index, value); @data[index] = value; end
12
- def map_foorth_shared(index); @data[index]; end
13
- end
14
-
15
- class MockObject
16
- def initialize; @data = {}; end
17
- def []=(index, value); @data[index] = value; end
18
- def map_foorth_exclusive(index); @data[index]; end
19
- end
20
-
21
- #Test the monkey patches applied to the Object class.
22
- class ContextTester < Minitest::Test
23
-
24
- #Track mini-test progress.
25
- include MinitestVisible
26
-
27
- def test_data_store
28
- context = XfOOrth::Context.new(45, stuff: 'buy', price: :plenty)
29
-
30
- assert_equal(context.previous, 45)
31
- assert_equal(context[:stuff], 'buy')
32
- assert_equal(context[:price], :plenty)
33
-
34
- context[:stuff] = 'sell'
35
- assert_equal(context[:stuff], 'sell')
36
- assert_equal(context[:price], :plenty)
37
-
38
- context[:price] = 9.95
39
- assert_equal(context[:stuff], 'sell')
40
- assert_equal(context[:price], 9.95)
41
- end
42
-
43
- #Test level counting
44
- def test_level_tracking
45
- context = XfOOrth::Context.new(nil, stuff: 'buy')
46
- assert_equal(context.depth, 1)
47
-
48
- context = XfOOrth::Context.new(context, stuff: 'other')
49
- assert_equal(context.depth, 2)
50
-
51
- context = XfOOrth::Context.new(context, stuff: 'more')
52
- assert_equal(context.depth, 3)
53
-
54
- context = context.previous
55
- assert_equal(context.depth, 2)
56
-
57
- context = context.previous
58
- assert_equal(context.depth, 1)
59
-
60
- context = context.previous
61
- assert_equal(context, nil)
62
- end
63
-
64
- def test_the_nesting_of_scopes
65
- context = XfOOrth::Context.new(nil, stuff: 'buy')
66
- assert_equal(context[:foo], nil)
67
- assert_equal(context[:jelly], nil)
68
- assert_equal(context[:stuff], 'buy')
69
- context[:foo] = 1
70
- context[:jelly] = 'donut'
71
- assert_equal(context[:jelly], 'donut')
72
- assert_equal(context[:stuff], 'buy')
73
- assert_equal(context[:foo], 1)
74
-
75
- context = XfOOrth::Context.new(context, stuff: 'other')
76
- assert_equal(context[:foo], 1)
77
- assert_equal(context[:jelly], 'donut')
78
- assert_equal(context[:stuff], 'other')
79
- context[:foo] = 2
80
- assert_equal(context[:jelly], 'donut')
81
- assert_equal(context[:stuff], 'other')
82
- assert_equal(context[:foo], 2)
83
-
84
- context = XfOOrth::Context.new(context, stuff: 'more')
85
- assert_equal(context[:foo], 2)
86
- assert_equal(context[:jelly], 'donut')
87
- assert_equal(context[:stuff], 'more')
88
- context[:foo] = 3
89
- context[:jelly] = 'Berliner'
90
- assert_equal(context[:jelly], 'Berliner')
91
- assert_equal(context[:stuff], 'more')
92
- assert_equal(context[:foo], 3)
93
-
94
- context = context.previous
95
- assert_equal(context[:foo], 2)
96
- assert_equal(context[:jelly], 'donut')
97
- assert_equal(context[:stuff], 'other')
98
-
99
- context = context.previous
100
- assert_equal(context[:foo], 1)
101
- assert_equal(context[:jelly], 'donut')
102
- assert_equal(context[:stuff], 'buy')
103
- end
104
-
105
- def test_the_local_mapping_of_symbols
106
- context = XfOOrth::Context.new(nil)
107
-
108
- name = 'b'
109
- sym = XfOOrth::SymbolMap.add_entry(name)
110
- context[sym] = XfOOrth::VmSpec.new(name, sym, [])
111
- spec = context.map(name)
112
- assert(spec.is_a?(XfOOrth::VmSpec))
113
- end
114
-
115
- def test_the_class_mapping_of_symbols
116
- mk = MockClass.new
117
- context = XfOOrth::Context.new(nil, cls: mk)
118
-
119
- name = '.c'
120
- sym = XfOOrth::SymbolMap.add_entry(name)
121
- mk[sym] = XfOOrth::TosSpec.new(name, sym, [])
122
- spec = context.map(name)
123
- assert(spec.is_a?(XfOOrth::TosSpec))
124
- end
125
-
126
- def test_the_exclusive_mapping_of_symbols
127
- mk = MockObject.new
128
- context = XfOOrth::Context.new(nil, obj: mk)
129
-
130
- name = '.d'
131
- sym = XfOOrth::SymbolMap.add_entry(name)
132
- mk[sym] = XfOOrth::TosSpec.new(name, sym, [])
133
- spec = context.map(name)
134
- assert(spec.is_a?(XfOOrth::TosSpec))
135
- end
136
-
137
- def test_that_it_verifies_sets
138
- context = XfOOrth::Context.new(nil, mode: :Execute, ctrl: :colon)
139
-
140
- assert(context.check_set(:mode, [:Execute, :Compile]))
141
-
142
- assert_raises(XfOOrth::XfOOrthError) do
143
- context.check_set(:mode, [:Compile, :Deferred])
144
- end
145
-
146
- assert(context.check_set(:stuff, [nil]))
147
- end
148
-
149
- def test_the_locating_of_the_receiver
150
- context = XfOOrth::Context.new(nil, vm: 'vm_sample')
151
- assert_equal('vm_sample', context.target)
152
-
153
- context = XfOOrth::Context.new(context, cls: 'cls_sample')
154
- assert_equal('cls_sample', context.target)
155
-
156
- context = XfOOrth::Context.new(context, obj: 'obj_sample')
157
- assert_equal('obj_sample', context.target)
158
-
159
- context = XfOOrth::Context.new(nil)
160
- assert_raises(XfOOrth::XfOOrthError) { context.target }
161
- end
162
-
163
- def test_adding_and_removing_local_methods
164
- context = XfOOrth::Context.new(nil, vm: 'vm_sample')
165
- name = 'lm'
166
- sym = XfOOrth::SymbolMap.add_entry(name)
167
- spec = context.create_local_method(name, XfOOrth::LocalSpec, [])
168
-
169
- assert_equal(spec, context[sym])
170
-
171
- context.remove_local_method(name)
172
-
173
- assert_equal(nil, context[sym])
174
-
175
- end
176
-
177
- end
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
+ class MockClass
10
+ def initialize; @data = {}; end
11
+ def []=(index, value); @data[index] = value; end
12
+ def map_foorth_shared(index); @data[index]; end
13
+ end
14
+
15
+ class MockObject
16
+ def initialize; @data = {}; end
17
+ def []=(index, value); @data[index] = value; end
18
+ def map_foorth_exclusive(index); @data[index]; end
19
+ end
20
+
21
+ #Test the monkey patches applied to the Object class.
22
+ class ContextTester < Minitest::Test
23
+
24
+ #Track mini-test progress.
25
+ include MinitestVisible
26
+
27
+ def test_data_store
28
+ context = XfOOrth::Context.new(45, stuff: 'buy', price: :plenty)
29
+
30
+ assert_equal(context.previous, 45)
31
+ assert_equal(context[:stuff], 'buy')
32
+ assert_equal(context[:price], :plenty)
33
+
34
+ context[:stuff] = 'sell'
35
+ assert_equal(context[:stuff], 'sell')
36
+ assert_equal(context[:price], :plenty)
37
+
38
+ context[:price] = 9.95
39
+ assert_equal(context[:stuff], 'sell')
40
+ assert_equal(context[:price], 9.95)
41
+ end
42
+
43
+ #Test level counting
44
+ def test_level_tracking
45
+ context = XfOOrth::Context.new(nil, stuff: 'buy')
46
+ assert_equal(context.depth, 1)
47
+
48
+ context = XfOOrth::Context.new(context, stuff: 'other')
49
+ assert_equal(context.depth, 2)
50
+
51
+ context = XfOOrth::Context.new(context, stuff: 'more')
52
+ assert_equal(context.depth, 3)
53
+
54
+ context = context.previous
55
+ assert_equal(context.depth, 2)
56
+
57
+ context = context.previous
58
+ assert_equal(context.depth, 1)
59
+
60
+ context = context.previous
61
+ assert_nil(context)
62
+ end
63
+
64
+ def test_the_nesting_of_scopes
65
+ context = XfOOrth::Context.new(nil, stuff: 'buy')
66
+ assert_nil(context[:foo])
67
+ assert_nil(context[:jelly])
68
+ assert_equal(context[:stuff], 'buy')
69
+ context[:foo] = 1
70
+ context[:jelly] = 'donut'
71
+ assert_equal(context[:jelly], 'donut')
72
+ assert_equal(context[:stuff], 'buy')
73
+ assert_equal(context[:foo], 1)
74
+
75
+ context = XfOOrth::Context.new(context, stuff: 'other')
76
+ assert_equal(context[:foo], 1)
77
+ assert_equal(context[:jelly], 'donut')
78
+ assert_equal(context[:stuff], 'other')
79
+ context[:foo] = 2
80
+ assert_equal(context[:jelly], 'donut')
81
+ assert_equal(context[:stuff], 'other')
82
+ assert_equal(context[:foo], 2)
83
+
84
+ context = XfOOrth::Context.new(context, stuff: 'more')
85
+ assert_equal(context[:foo], 2)
86
+ assert_equal(context[:jelly], 'donut')
87
+ assert_equal(context[:stuff], 'more')
88
+ context[:foo] = 3
89
+ context[:jelly] = 'Berliner'
90
+ assert_equal(context[:jelly], 'Berliner')
91
+ assert_equal(context[:stuff], 'more')
92
+ assert_equal(context[:foo], 3)
93
+
94
+ context = context.previous
95
+ assert_equal(context[:foo], 2)
96
+ assert_equal(context[:jelly], 'donut')
97
+ assert_equal(context[:stuff], 'other')
98
+
99
+ context = context.previous
100
+ assert_equal(context[:foo], 1)
101
+ assert_equal(context[:jelly], 'donut')
102
+ assert_equal(context[:stuff], 'buy')
103
+ end
104
+
105
+ def test_getting_context_with_a_specific_tag
106
+ c1 = XfOOrth::Context.new(nil, ctrl: :procedure)
107
+ c2 = XfOOrth::Context.new(c1, ctrl: :if)
108
+ c3 = XfOOrth::Context.new(c2, ctrl: :loop)
109
+
110
+ assert_equal(c1, c3.get_context_by_ctrl(:procedure))
111
+ assert_equal(c2, c3.get_context_by_ctrl(:if))
112
+ assert_equal(c3, c3.get_context_by_ctrl(:loop))
113
+ assert_raises(XfOOrth::XfOOrthError) { c3.get_context_by_ctrl(:wrong) }
114
+ end
115
+
116
+ def test_the_local_mapping_of_symbols
117
+ context = XfOOrth::Context.new(nil)
118
+
119
+ name = 'b'
120
+ sym = XfOOrth::SymbolMap.add_entry(name)
121
+ context[sym] = XfOOrth::VmSpec.new(name, sym, [])
122
+ spec = context.map_with_defaults(name)
123
+ assert(spec.is_a?(XfOOrth::VmSpec))
124
+ end
125
+
126
+ def test_the_class_mapping_of_symbols
127
+ mk = MockClass.new
128
+ context = XfOOrth::Context.new(nil, cls: mk)
129
+
130
+ name = '.c'
131
+ sym = XfOOrth::SymbolMap.add_entry(name)
132
+ mk[sym] = XfOOrth::TosSpec.new(name, sym, [])
133
+ spec = context.map_with_defaults(name)
134
+ assert(spec.is_a?(XfOOrth::TosSpec))
135
+ end
136
+
137
+ def test_the_exclusive_mapping_of_symbols
138
+ mk = MockObject.new
139
+ context = XfOOrth::Context.new(nil, obj: mk)
140
+
141
+ name = '.d'
142
+ sym = XfOOrth::SymbolMap.add_entry(name)
143
+ mk[sym] = XfOOrth::TosSpec.new(name, sym, [])
144
+ spec = context.map_with_defaults(name)
145
+ assert(spec.is_a?(XfOOrth::TosSpec))
146
+ end
147
+
148
+ def test_that_it_verifies_sets
149
+ context = XfOOrth::Context.new(nil, mode: :Execute, ctrl: :colon)
150
+
151
+ assert(context.check_set(:mode, [:Execute, :Compile]))
152
+
153
+ assert_raises(XfOOrth::XfOOrthError) do
154
+ context.check_set(:mode, [:Compile, :Deferred])
155
+ end
156
+
157
+ assert(context.check_set(:stuff, [nil]))
158
+ end
159
+
160
+ def test_the_locating_of_the_receiver
161
+ context = XfOOrth::Context.new(nil, vm: 'vm_sample')
162
+ assert_equal('vm_sample', context.target)
163
+
164
+ context = XfOOrth::Context.new(context, cls: 'cls_sample')
165
+ assert_equal('cls_sample', context.target)
166
+
167
+ context = XfOOrth::Context.new(context, obj: 'obj_sample')
168
+ assert_equal('obj_sample', context.target)
169
+
170
+ context = XfOOrth::Context.new(nil)
171
+ assert_raises(XfOOrth::XfOOrthError) { context.target }
172
+ end
173
+
174
+ def test_adding_and_removing_local_methods
175
+ context = XfOOrth::Context.new(nil, vm: 'vm_sample')
176
+ name = 'lm'
177
+ sym = XfOOrth::SymbolMap.add_entry(name)
178
+ spec = context.create_local_method(name, XfOOrth::LocalSpec, [])
179
+
180
+ assert_equal(spec, context[sym])
181
+
182
+ context.remove_local_method(name)
183
+
184
+ assert_nil(context[sym])
185
+
186
+ end
187
+
188
+ end