Module: Nasl::Test

Defined in:
lib/nasl/test.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.initialize!(args) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/nasl/test.rb', line 32

def self.initialize!(args)
  # Run all tests by default.
  args = ['unit/*', 'unit/*/*'] if args.empty?

  # Run each test or category of tests specified on the command line.
  args.each do |test|
    Dir.glob(Nasl.test + (test + '.rb')).each { |f| load(f) }
  end
end

Instance Method Details

#context(code) ⇒ Object



54
55
56
# File 'lib/nasl/test.rb', line 54

def context(code)
  Nasl::Context.new(code, "(test)")
end

#diff(code, kg_tree) ⇒ Object



78
79
80
81
82
# File 'lib/nasl/test.rb', line 78

def diff(code, kg_tree)
  tree = parse(code)
  assert_not_nil(tree)
  assert_not_equal(flatten(kg_tree), flatten(tree.to_s)) if !tree.nil?
end

#fail(code) ⇒ Object



58
59
60
61
62
# File 'lib/nasl/test.rb', line 58

def fail(code)
  tree = parse(code)

  assert_nil(tree)
end

#fail_parse(code) ⇒ Object



64
65
66
# File 'lib/nasl/test.rb', line 64

def fail_parse(code)
  assert_raise(ParseException) { Nasl::Parser.new.parse(code) }
end

#fail_token(code) ⇒ Object



68
69
70
# File 'lib/nasl/test.rb', line 68

def fail_token(code)
  assert_raise(TokenException) { Nasl::Parser.new.parse(code) }
end

#flatten(tree) ⇒ Object



42
43
44
# File 'lib/nasl/test.rb', line 42

def flatten(tree)
  tree.chomp.split(/\n/).map(&:strip).join
end

#parse(code) ⇒ Object



46
47
48
# File 'lib/nasl/test.rb', line 46

def parse(code)
  Nasl::Parser.new.parse(code, "(test)")
end

#pass(code) ⇒ Object



72
73
74
75
76
# File 'lib/nasl/test.rb', line 72

def pass(code)
  tree = parse(code)

  assert_not_nil(tree)
end

#same(code, kg_tree) ⇒ Object



84
85
86
87
88
# File 'lib/nasl/test.rb', line 84

def same(code, kg_tree)
  tree = parse(code)
  assert_not_nil(tree)
  assert_equal(flatten(kg_tree), flatten(tree.to_s)) if !tree.nil?
end

#tokenize(code) ⇒ Object



50
51
52
# File 'lib/nasl/test.rb', line 50

def tokenize(code)
  Nasl::Tokenizer.new(code, "(test)")
end