Class: Jasmine::SpecBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/jasmine/spec_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ SpecBuilder

Returns a new instance of SpecBuilder.



7
8
9
10
11
12
# File 'lib/jasmine/spec_builder.rb', line 7

def initialize(config)
  @config = config
  @spec_files = config.spec_files
  @runner = config
  @spec_ids = []
end

Instance Attribute Details

#suitesObject

Returns the value of attribute suites.



5
6
7
# File 'lib/jasmine/spec_builder.rb', line 5

def suites
  @suites
end

Instance Method Details

#declare_spec(parent, spec) ⇒ Object



107
108
109
110
111
112
113
114
115
# File 'lib/jasmine/spec_builder.rb', line 107

def declare_spec(parent, spec)
  me = self
  example_name = spec["name"]
  @spec_ids << spec["id"]
  backtrace = @example_locations[parent.description + " " + example_name]
  parent.it example_name, {}, backtrace do
    me.report_spec(spec["id"])
  end
end

#declare_suite(parent, suite) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/jasmine/spec_builder.rb', line 91

def declare_suite(parent, suite)
  me = self
  parent.describe suite["name"] do
    suite["children"].each do |suite_or_spec|
      type = suite_or_spec["type"]
      if type == "suite"
        me.declare_suite(self, suite_or_spec)
      elsif type == "spec"
        me.declare_spec(self, suite_or_spec)
      else
        raise "unknown type #{type} for #{suite_or_spec.inspect}"
      end
    end
  end
end

#declare_suitesObject



84
85
86
87
88
89
# File 'lib/jasmine/spec_builder.rb', line 84

def declare_suites
  me = self
  suites.each do |suite|
    declare_suite(self, suite)
  end
end

#guess_example_locationsObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/jasmine/spec_builder.rb', line 30

def guess_example_locations
  @example_locations = {}

  example_name_parts = []
  previous_indent_level = 0
  @config.spec_files_full_paths.each do |filename|
    line_number = 1
    File.open(filename, "r") do |file|
      file.readlines.each do |line|
        match = /^(\s*)(describe|it)\s*\(\s*["'](.*)["']\s*,\s*function/.match(line)
        if (match)
          indent_level = match[1].length / 2
          example_name = match[3]
          example_name_parts[indent_level] = example_name

          full_example_name = example_name_parts.slice(0, indent_level + 1).join(" ")
          @example_locations[full_example_name] = "#{filename}:#{line_number}: in `it'"
        end
        line_number += 1
      end
    end
  end
end

#load_resultsObject



69
70
71
72
73
74
75
# File 'lib/jasmine/spec_builder.rb', line 69

def load_results
  @spec_results = {}
  @spec_ids.each_slice(50) do |slice|
    @spec_results.merge!(eval_js("JSON.stringify(jsApiReporter.resultsForSpecs(#{JSON.generate(slice)}))"))
  end
  @spec_results
end

#load_suite_infoObject



54
55
56
57
58
59
60
61
62
# File 'lib/jasmine/spec_builder.rb', line 54

def load_suite_info
  started = Time.now
  while !eval_js('jsApiReporter && jsApiReporter.started') do
    raise "couldn't connect to Jasmine after 60 seconds" if (started + 60 < Time.now)
    sleep 0.1
  end

  @suites = eval_js('JSON.stringify(jsApiReporter.suites())')
end

#report_spec(spec_id) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/jasmine/spec_builder.rb', line 117

def report_spec(spec_id)
  spec_results = results_for(spec_id)

  out = ""
  messages = spec_results['messages'].each do |message|
    case
      when message["type"] == "MessageResult"
        puts message["text"]
        puts "\n"
      else
        unless message["message"] =~ /^Passed.$/
          STDERR << message["message"]
          STDERR << "\n"

          out << message["message"]
          out << "\n"
        end

        if !message["passed"] && message["trace"]["stack"]
          stack_trace = message["trace"]["stack"].gsub(/<br \/>/, "\n").gsub(/<\/?b>/, " ")
          STDERR << stack_trace.gsub(/\(.*\)@http:\/\/localhost:[0-9]+\/specs\//, "/spec/")
          STDERR << "\n"
        end
    end

  end
  fail out unless spec_results['result'] == 'passed'
  puts out unless out.empty?
end

#results_for(spec_id) ⇒ Object



64
65
66
67
# File 'lib/jasmine/spec_builder.rb', line 64

def results_for(spec_id)
  @spec_results ||= load_results
  @spec_results[spec_id.to_s]
end

#script_pathObject



26
27
28
# File 'lib/jasmine/spec_builder.rb', line 26

def script_path
  File.expand_path(__FILE__)
end

#startObject



14
15
16
17
18
19
20
# File 'lib/jasmine/spec_builder.rb', line 14

def start
  guess_example_locations

  @runner.start
  load_suite_info
  wait_for_suites_to_finish_running
end

#stopObject



22
23
24
# File 'lib/jasmine/spec_builder.rb', line 22

def stop
  @runner.stop
end

#wait_for_suites_to_finish_runningObject



77
78
79
80
81
82
# File 'lib/jasmine/spec_builder.rb', line 77

def wait_for_suites_to_finish_running
  puts "Waiting for suite to finish in browser ..."
  while !eval_js('jsApiReporter.finished') do
    sleep 0.1
  end
end