Permalink
Browse files

Implement up to Test Cases

  • Loading branch information...
1 parent 106f891 commit a94017a35e9b54886b1b8fa508070838fc347748 @relaxdiego committed May 30, 2012
@@ -0,0 +1,7 @@
+def my_username
+ 'test'
+end
+
+def my_password
+ 'test'
+end
@@ -0,0 +1,3 @@
+Steps.add /^I have a role of (.+) in the system$/ do |role_name|
+
+end
View
@@ -1,14 +1,45 @@
+require_relative 'steps'
require_relative 'test_case_translator'
+require_relative 'test_cases'
module Norm
+ RED = "\033[0;31m"
+ YELLOW = "\033[0;33m"
+ GREEN = "\033[0;32m"
+ NORMAL = "\033[m"
+
class Runtime
- attr_reader :root_path, :output_path, :test_cases_path
+ attr_reader :directives_path,
+ :utilities_path,
+ :test_cases_path,
+ :requirements_path,
+ :output_path
+
+ def initialize(directives_path)
+ @directives_path = directives_path
+
+ @utilities_path = File.join(directives_path, 'utilities')
+ @test_cases_path = File.join(directives_path, 'test_cases')
+ @output_path = File.join(directives_path, '..', 'output')
+ end
+
+ def load_utilities(path = utilities_path)
+ Dir.entries(path).each do |entry_name|
+ next if entry_name =~ /^\.\.?$/
+
+ if entry_name =~ /^.+\.rb$/
+ require_relative File.join(path, entry_name)
+ elsif File.directory?(path)
+ load_utilities(File.join(path, entry_name))
+ end
+ end
+ end
- def initialize(root_path)
- @root_path = root_path
- @output_path = File.join(root_path, 'output')
- @test_cases_path = File.join(root_path, 'test_cases')
+ def load_test_cases
+ Dir.entries(output_path).each do |file_name|
+ require_relative File.join(output_path, file_name) if file_name =~ /^.+\.test_cases\.rb$/
+ end
end
def process_test_cases
@@ -27,6 +58,13 @@ def process_test_cases
end
end
+ def start
+ load_utilities
+
+ process_test_cases
+ load_test_cases
+ end
+
private
def ensure_output_dir
View
@@ -0,0 +1,48 @@
+require 'singleton'
+
+module Norm
+ class Steps
+ include Singleton
+
+ def self.add(regex, &block)
+ instance.add(regex, &block)
+ end
+
+ def self.call(string)
+ instance.call(string)
+ end
+
+ attr_reader :items
+
+ def initialize
+ @items = {}
+ end
+
+ def add(regex, &block)
+ items[regex] = block
+ end
+
+ def call(string)
+ matched_items = []
+ items.each do |regex, block|
+ matched_items << { :regex => regex, :block => block } if regex.match(string)
+ end
+
+ if matched_items.size == 1
+ puts " #{ YELLOW }Step #{ GREEN }#{string}#{ NORMAL }"
+ item = matched_items[0]
+ vars = item[:regex].match(string)
+
+ if vars.length > 1
+ item[:block].call(*vars[1..-1])
+ else
+ item[:block].call
+ end
+ elsif matched_items.size > 1
+ raise "Ambiguous step match"
+ else
+ puts " #{ YELLOW }Step #{ RED }#{ string } (undefined)#{ NORMAL }"
+ end
+ end
+ end
+end
@@ -51,7 +51,7 @@ def self.translate(string)
in_cleanup = false
if type == TEST_CASE
- ruby_code << "test_cases.add(/^"
+ ruby_code << "TestCases.add(/^"
in_header = true
elsif type == VARIABLES
in_variables = true
@@ -78,7 +78,7 @@ def self.translate(string)
position += variable_def.size
elsif (in_preconditions || in_script || in_cleanup) && step = chunk[/\A(\s*\**\s*.+)/, 1]
str = step.gsub(/\A\s*\**\s*/, '')
- ruby_code << " steps.call(\"#{ translate_vars(str, variables) }\")\n"
+ ruby_code << " Steps.call(\"#{ translate_vars(str, variables) }\")\n"
position += step.size
end
View
@@ -1,5 +1,17 @@
+require 'singleton'
+
module Norm
class TestCases
+ include Singleton
+
+ def self.add(regex, &block)
+ instance.add(regex, &block)
+ end
+
+ def self.call(string)
+ instance.call(string)
+ end
+
attr_reader :items
def initialize
@@ -17,12 +29,13 @@ def call(string)
end
if matches.size == 1
+ puts "\n #{ YELLOW }TestCase #{ GREEN }#{string}#{ NORMAL }"
match = matches[0]
match[:block].call(match[:regex], string)
elsif matches.size > 1
- raise "Ambiguous match"
+ raise "Ambiguous test case match"
else
- puts "Undefined test case '#{ string }'"
+ puts "\n #{ YELLOW }TestCase #{ RED }#{string} (undefined)#{ NORMAL }"
end
end
end
View
11 norm
@@ -0,0 +1,11 @@
+#!/usr/bin/env ruby
+require_relative 'lib/runtime'
+
+include Norm
+
+directives_path = File.expand_path("../directives", __FILE__)
+runtime = Runtime.new(directives_path)
+runtime.start
+
+TestCases.call "A user with a role of System Admin in the system can create a project"
+TestCases.call "A user"
View
@@ -9,10 +9,10 @@ def test_processing_of_test_cases
norm = Runtime.new(root_path)
norm.process_test_cases
- assert Dir.exists?(File.join(root_path, 'output')), 'Output directory was not created'
+ assert Dir.exists?(File.join(root_path, '..', 'output')), 'Output directory was not created'
Dir.entries(File.join(root_path, 'test_cases')).each do |entry|
- rb_file_path = File.join(root_path, 'output', entry.gsub('.test_cases', '.test_cases.rb'))
+ rb_file_path = File.join(root_path, '..', 'output', entry.gsub('.test_cases', '.test_cases.rb'))
assert File.exists?(rb_file_path), "#{ rb_file_path } does not exist"
end
end
@@ -0,0 +1,3 @@
+steps.add(/^I have a role of (.+) in the system$/) do |role_name|
+
+end
Oops, something went wrong.

0 comments on commit a94017a

Please sign in to comment.