# # test/porolog/arguments_test.rb - Test Suite for Porolog::Arguments # # Luis Esteban 2 May 2018 # created # require_relative '../test_helper' describe 'Porolog' do before(:all) do reset end let(:pred_name) { :pred } let(:pred) { Predicate.new pred_name } let(:pred1) { Predicate.new :p } let(:pred2) { Predicate.new :q } describe 'Arguments' do describe '.reset' do it 'should delete/unregister all Arguments' do args1 = Arguments.new pred, [1,:x,'word',[2,:y,0.3]] args2 = Arguments.new pred, [2] args3 = Arguments.new pred, [3,:x,:y,:z] assert_equal 'Arguments1', args1.myid assert_equal 'Arguments2', args2.myid assert_equal 'Arguments3', args3.myid Arguments.reset args4 = Arguments.new pred, [4,[1,2,3]] args5 = Arguments.new pred, [5,[]] assert_equal 'Arguments-999', args1.myid assert_equal 'Arguments-999', args2.myid assert_equal 'Arguments-999', args3.myid assert_equal 'Arguments1', args4.myid assert_equal 'Arguments2', args5.myid Arguments.reset assert_equal 'Arguments-999', args1.myid assert_equal 'Arguments-999', args2.myid assert_equal 'Arguments-999', args3.myid assert_equal 'Arguments-999', args4.myid assert_equal 'Arguments-999', args5.myid end end describe '.new' do it 'should create a new Arguments' do arguments = Arguments.new pred, [1, :x, 'word', [2, :y, 0.3]] assert_Arguments arguments, :pred, [1, :x, 'word', [2, :y, 0.3]] end end describe '.arguments' do it 'should return all registered arguments' do # -- No Arguments -- assert_equal 0, Arguments.arguments.size # -- One Arguments -- arguments1 = Arguments.new pred1, [:x,:y,:z] assert_equal 1, Arguments.arguments.size assert_Arguments Arguments.arguments.last, :p, [:x,:y,:z] # -- Two Arguments -- arguments2 = Arguments.new pred2, [:a,:b,:c,:d] assert_equal 2, Arguments.arguments.size assert_Arguments Arguments.arguments.last, :q, [:a,:b,:c,:d] assert_equal [arguments1, arguments2], Arguments.arguments end end describe '#initialize' do it 'should initialize predicate and arguments' do arguments = Arguments.new pred, [:x,:y,:z] assert_equal pred, arguments.predicate assert_equal [:x,:y,:z], arguments.arguments end it 'should register the arguments' do arguments1 = Arguments.new pred, [:x] arguments2 = Arguments.new pred, [:x,:y] arguments3 = Arguments.new pred, [:x,:y,:z] assert_equal [ arguments1, arguments2, arguments3, ], Arguments.arguments end end describe '#myid' do it 'should return its class and index as a String' do arguments1 = Arguments.new pred, [:x] arguments2 = Arguments.new pred, [:x,:y] arguments3 = Arguments.new pred, [:x,:y,:z] assert_equal 'Arguments1', arguments1.myid assert_equal 'Arguments2', arguments2.myid assert_equal 'Arguments3', arguments3.myid end it 'should return its class and -999 as a String when deleted/reset' do arguments1 = Arguments.new pred, [:x] arguments2 = Arguments.new pred, [:x,:y] arguments3 = Arguments.new pred, [:x,:y,:z] Arguments.reset assert_equal 'Arguments-999', arguments1.myid assert_equal 'Arguments-999', arguments2.myid assert_equal 'Arguments-999', arguments3.myid end end describe '#inspect' do it 'should show the predicate and arguments' do predicate1 = Predicate.new :p predicate2 = Predicate.new :q predicate3 = Predicate.new :generic arguments1 = Arguments.new predicate1, [:x] arguments2 = Arguments.new predicate2, [:list, [1,2,3]] arguments3 = Arguments.new predicate3, [:a,:b,:c] assert_equal 'p(:x)', arguments1.inspect assert_equal 'q(:list,[1, 2, 3])', arguments2.inspect assert_equal 'generic(:a,:b,:c)', arguments3.inspect end end describe '#fact!' do it 'should create a fact for its predicate' do arguments1 = pred.(1,'a',0.1) arguments1.fact! assert_equal '[ pred(1,"a",0.1):- true]', pred.rules.inspect end end describe '#fallacy!' do it 'should create a fallacy for its predicate' do arguments1 = pred.(1,'a',0.1) arguments1.fallacy! assert_equal '[ pred(1,"a",0.1):- false]', pred.rules.inspect end end describe '#cut_fact!' do it 'should create a fact for its predicate and terminate solving the goal' do arguments1 = pred.(1,'a',0.1) arguments1.cut_fact! assert_equal '[ pred(1,"a",0.1):- [:CUT, true]]', pred.rules.inspect end end describe '#cut_fallacy!' do it 'should create a fallacy for its predicate and terminate solving the goal' do arguments1 = pred.(1,'a',0.1) arguments1.cut_fallacy! assert_equal '[ pred(1,"a",0.1):- [:CUT, false]]', pred.rules.inspect end end describe '#<<' do it 'should create a rule for its predicate' do arguments1 = pred.(1,'a',0.1) arguments1 << [ pred1.(1,2,3) ] assert_equal '[ pred(1,"a",0.1):- [p(1,2,3)]]', pred.rules.inspect end end describe '#evaluates' do it 'should add a block as a rule to its predicate' do arguments1 = pred.(1,'a',0.1) line = __LINE__ ; arguments2 = arguments1.evaluates do |*args| #:nocov: $stderr.puts "args = #{args.inspect}" #:nocov: end assert_instance_of Arguments, arguments2 part1 = "[ pred(1,\"a\",0.1):- #