Skip to content

Commit

Permalink
Compare JS describes to Ruby describes
Browse files Browse the repository at this point in the history
  • Loading branch information
bootstraponline committed Jun 5, 2015
1 parent 1ff1018 commit 186618b
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 29 deletions.
8 changes: 8 additions & 0 deletions Thorfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,12 @@ class ::Default < Thor

# only the first exec will work so we can't use two of them.
end

desc 'compare', 'Compare protractor JS specs to ruby specs'
def compare
commands = [
'rspec ./gen/compare_specs.rb'
].join ';'
exec commands
end
end
80 changes: 80 additions & 0 deletions gen/compare_specs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
require 'rubygems'
require 'rspec'
require 'pry'

=begin
rspec ./gen/compare_specs.rb
=end

describe 'compare ruby and protractor specs' do
def ruby_to_js_rel path
path.sub(ruby_protractor_spec_folder, '').sub(/\.rb\Z/, '.js')
end

def base_path
@base_path ||= File.expand_path File.join(__dir__, '..')
end

def ruby_protractor_spec_folder
@ruby_protractor_spec_folder ||= File.expand_path File.join(base_path, 'spec', 'upstream')
end

before :all do
ruby_protractor_files = Dir.glob ruby_protractor_spec_folder + '/**/*.rb'
ruby_protractor_files.map! &File.method(:expand_path)

js_protractor_spec_folder = File.expand_path File.join(base_path, 'protractor/spec')

# [[ruby_spec, js_spec]]
spec_pairs = []

# validate all Ruby protractor specs have matching JavaScript protractor specs
ruby_protractor_files.each do |spec|
rb_spec_relative = ruby_to_js_rel spec

js_spec = File.join(js_protractor_spec_folder, rb_spec_relative)
raise "Matching protractor spec does not exist\n#{js_spec}" unless File.exist?(js_spec)

spec_pairs << [spec, js_spec]
end

@spec_pairs = spec_pairs
end

def spec_pairs
@spec_pairs
end

# Ruby specs must have the same describes as the JS files.
it 'ruby specs have idential describes to protractor specs' do
spec_pairs.each do |ruby_spec, js_spec|
puts "Comparing: #{ruby_to_js_rel(ruby_spec)}"
js_spec = File.read js_spec
ruby_spec = File.read ruby_spec

# match describes
js_describes = js_spec.scan(/describe\W*\(\W*['|"](.*?)['|"]\W*,/).flatten
ruby_describes = ruby_spec.scan(/describe\W*['|"](.*?)['|"]\W*do/).flatten

difference = js_describes - ruby_describes
none = []

# redundant checks for more human readable rspec failure messages
expect(difference).to eq(none)
expect(js_describes.length).to eq(ruby_describes.length)

# expect order to be the same
js_describes.each_with_index do |describe, index|
begin
expect(describe).to eq(ruby_describes[index])
rescue
puts js_describes.join("\n")
raise
end
end

# and they should be identical
expect(js_describes).to eq(ruby_describes)
end
end
end
4 changes: 3 additions & 1 deletion spec/by_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,8 @@ def by_methods
should_not
]

expect_equal Object.methods.sort, method_whitelist.sort
difference = Object.methods - method_whitelist
none = []
expect(difference).to eq(none)
end
end
4 changes: 2 additions & 2 deletions spec/upstream/basic/elements_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,6 @@ def newExpected expectedLabel

# Ruby has no shortcut css notation ($$ and $ are JS only)
#
# describe 'shortcut css notation'
# describe 'shortcut css notation' do
# it 'should grab by css'
# it 'should chain $$ with $' do
# it 'should chain $$ with $' do
52 changes: 26 additions & 26 deletions spec/upstream/basic/locators_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,32 +146,6 @@
end
end

describe 'by css containing text' do
it 'should find elements by css and partial text' do
arr = element.all(by.cssContainingText('#animals ul .pet', 'dog')).to_a
expect(arr.length).to eq(2)
expect(arr[0].id).to eq('bigdog')
expect(arr[1].id).to eq('smalldog')
end

it 'should find elements with text-transform style' do
selector = '#transformedtext div'
expect(element(by.cssContainingText(selector, 'Uppercase')).id).to eq('textuppercase')
expect(element(by.cssContainingText(selector, 'Lowercase')).id).to eq('textlowercase')
expect(element(by.cssContainingText(selector, 'capitalize')).id).to eq('textcapitalize')
end
end

describe 'by options' do
it 'should find elements by options' do
allOptions = element.all(by.options('fruit for fruit in fruits')).to_a
expect(allOptions.length).to eq(4)

firstOption = allOptions.first
expect(firstOption.text).to eq('apple')
end
end

describe 'by repeater' do
before do
visit 'repeater'
Expand Down Expand Up @@ -316,6 +290,32 @@
end # describe 'repeaters using ng-repeat-start and ng-repeat-end'
end # describe 'by repeater'

describe 'by css containing text' do
it 'should find elements by css and partial text' do
arr = element.all(by.cssContainingText('#animals ul .pet', 'dog')).to_a
expect(arr.length).to eq(2)
expect(arr[0].id).to eq('bigdog')
expect(arr[1].id).to eq('smalldog')
end

it 'should find elements with text-transform style' do
selector = '#transformedtext div'
expect(element(by.cssContainingText(selector, 'Uppercase')).id).to eq('textuppercase')
expect(element(by.cssContainingText(selector, 'Lowercase')).id).to eq('textlowercase')
expect(element(by.cssContainingText(selector, 'capitalize')).id).to eq('textcapitalize')
end
end

describe 'by options' do
it 'should find elements by options' do
allOptions = element.all(by.options('fruit for fruit in fruits')).to_a
expect(allOptions.length).to eq(4)

firstOption = allOptions.first
expect(firstOption.text).to eq('apple')
end
end

# Don't even run shadow dom tests unless we're on chrome.
if browser_name == :chrome
describe 'by deep css' do
Expand Down

0 comments on commit 186618b

Please sign in to comment.