=begin
To learn to install Javascript::PurePerl, see:
http://phlip.eblogs.com/2007/07/28/javascriptpureperl-for-ruby-enthusiasts
# ERGO put cases in here for all the new assert_* features
=end
# CONSIDER why are these lines needed?
require 'test/unit'
rr = File.expand_path(File.dirname(__FILE__) + '/../../../../')
RAILS_ROOT = rr unless defined? RAILS_ROOT
require File.dirname(__FILE__) + '/../lib/yar_wiki.rb'
if RAILS_ENV == 'test'
require 'ostruct'
require 'hpricot'
module YarWikiTestResources
include FormTestHelper
def set_wiki_test_up
source = "--- !omap
- setup:
- !omap
- script: \"rjs.assert '!/Exception/.test(document.title)'\"
- test_ClickLink:
- !omap
- color: green
- page: /wiki/FrontPage
- script: |-
rjs.surf '$(\"link_RecentChanges\").href' do
rjs.assert '/RecentChanges$/.test(window.location.href)'
end
- test_WikiTestPage:
- !omap
- color: green
- page: /wiki/WikiTestPage
- script: |-
rjs.click 'link_node:test_uncle_wiggily'
rjs.assert_ajax /^\\/wiki\\/find_faults.*test_uncle_wiggily/ do
rjs.assert '$(\"title_node:test_uncle_wiggily\").className == \"wiki_red\"'
end
# something tests uncle wiggily
- teardown:
- !omap
- script: \"\"
"
@yar = YarWiki.new('TestWiki')
@yar.save_page(source)
end
# YarWiki-specific test resources here...
def get_omap
return "--- !omap
- setup:
- !omap
- script: \"rjs.assert '!/Exception caught/.test(document.title)'\"
- test_uncle_wiggily:
- !omap
- color: green
- page: /character/uncle_wiggily
- script: \"\# something tests uncle wiggily\"
- test_hammy_squirrel:
- !omap
- color: green
- page: /character/hammy_squirrel
- script: \"\# something tests hammy squirrel\"
- teardown:
- !omap
- script: rollback
"
end
# shunt this so we don't need to drag in most (but not all) of ActiveView...
def link_to_remote(*x)
return x.first.to_s
end
end
class YarWikiTest < Test::Unit::TestCase
include YarWikiTestResources
def yaml_to_xhtml(source)
@yar = YarWiki.new('WikiTestPage')
@yar.save_page(source)
x = Builder::XmlMarkup.new
@yar.format_yaml(self, x)
assert_xml x.target!
end
def test_yar_wiki
yar = YarWiki.new
assert_equal 'FrontPage', yar.page_name
yar = YarWiki.new('AnotherPage')
assert_equal 'AnotherPage', yar.page_name
end
def test_format_yaml
yar = YarWiki.new('WikiTestPage')
yar.save_page "--- !omap\n- key2: value2\n- key1: value1"
x = Builder::XmlMarkup.new
yar.format_yaml(self, x)
assert_xml x.target!, '/ul/li/table/tr/td/strong[ . = "key2" ]'
end
def test_learning_about_omap
o = YAML::Omap.new([['foo', 'aa'], ['bar', 'bb']])
omap = YAML.dump(o)
#p omap
o = YAML::Omap.new([['foo', o], ['bar', o]])
omap = YAML.dump(o)
#p omap
# doc o
sauce = "--- !omap
- a:
- !omap
- sku : BL394D
- quantity : 4
- description : Basketball
- b: two"
o = YAML::parse(sauce)
assert_equal 3, o.emit.split('omap').size
assert_equal 'BL394D', o.select('/0/a/0/0/sku')[0].value
assert_equal 'BL394D', o.select('//a//sku')[0].value
sauce = '!omap - Little: Nemo'
o = YAML::parse(sauce)
assert_equal :seq, o.kind
assert_equal "--- !omap \n- Little: Nemo\n", o.emit
o = YAML::parse(get_omap)
assert_equal 6, o.emit.split('!omap').size
end
def test_two_level_omaps
yar = YarWiki.new('WikiTestPage')
yar.save_page(get_omap)
x = Builder::XmlMarkup.new
yar.format_yaml(self, x)
assert_xml x.target!, '/ul/li/table/tr/td/strong[ . = "test_hammy_squirrel" ]' do
assert_xpath '../../../../ul/li/table/tr/td/strong[ . = "script" ]' do
assert_xpath '../../../../div[ . = "# something tests hammy squirrel" ]'
end
end
end
def test_select
yar = YarWiki.new('WikiTestPage')
yar.save_page(get_omap)
assert_match "document.title)'\n# something",
yar.select_value('//test_hammy_squirrel', '//script')
assert_equal "/character/uncle_wiggily",
yar.select_value('//test_uncle_wiggily', '//page')
end
def test_ypath_IDs
yaml_to_xhtml(get_omap)
assert_tag_id '/ul/li[3]/ul/li[2]/div', 'node:test_hammy_squirrel:script'
end
def test_following_sibling
assert_xml 'yodude'
# CONSIDER shouldn't this work?
#assert_equal 'dude', assert_xpath('/a/b/sibling::c[2]').text
end
def test_ondblclick
yaml_to_xhtml(get_omap)
hammy_id = 'node:test_hammy_squirrel:script'
assert_tag_id :div, hammy_id do |div|
assert_equal hammy_id, div[:id]
ondblclick = div[:ondblclick]
assert_match hammy_id, ondblclick
assert_match 'new Ajax.Updater', ondblclick
end
end
def test_assert_javascript
yaml_to_xhtml(get_omap)
hammy_id = 'node:test_hammy_squirrel:script'
source = @yar.ondblclick(hammy_id)
return unless got_pure_perl?
assert_javascript source
assert_equal hammy_id, assert_xpath('Statement[1]//String').text
assert_xpath 'Statement[3]//ArgumentList[1]' do
assert_equal 'ypath', assert_js_argument(1)
assert_equal '/wiki/edit_node', assert_js_argument(2)
json = assert_js_argument(3)
assert_equal true, json[:asynchronous]
assert_equal true, json[:evalScripts]
assert_equal 'get', json[:method]
end
end
def test_format_scalar
YarWiki.maybe_install_startup_files
yar = YarWiki.new
yar.x = Builder::XmlMarkup.new
str = YAML::Syck::Scalar.new('yo', 'RecentChanges', 'yo')
yar.format_scalar(str, 'node:yo')
# puts x.public_methods!.sort
assert_xml yar.x.target!, 'div/a[ @href = "RecentChanges" ]' do |a|
assert_equal 'RecentChanges', a.text
assert_equal 'link_RecentChanges', a[:id]
end
end
end
module YarWikiControllerTest
include YarWikiTestResources
# CONSIDER test RJS without the controller!
# CONSIDER run these with any controller,
# not just YarWiki's home projects' controller
def get_rjs
#:stopdoc:
ActionView::Base.debug_rjs = false #:nodoc:
ahp = ActionView::Helpers::PrototypeHelper #:nodoc:
ahp::JavaScriptGenerator.new(@controller) do |rjs|
return rjs
end
#:startdoc:
end
def test_javascript_tag
assert_no_match /&/, javascript_tag("document.write('&');")
end
def test_raw_Hpricot
get :index, :id => 'FrontPage' # serve a WikiWiki
doc = Hpricot(@response.body) # read the mock server response
script = doc.search('script[3]').first # locate our target ')
end
end
def test_forgiven_REXML_in_string
REXML::Document.new('')
end
def test_forgiven_REXML
script = javascript_tag(
'function find_faults(page_name, ypath)
{
var transcript = encodeURIComponent($("transcript").innerHTML);
var opts = { method: "get",
parameters: "page_name=" + page_name +
"&ypath=" + ypath +
"&transcript=" + transcript };
new Ajax.Request("/wiki/find_faults", opts);
}')
REXML::Document.new(script) # no raise
end
if got_pure_perl?
def test_ultimate_monkey_patch
yar = YarWiki.new('NotUsed')
# adding a missing_method, to supply magic members,
# to an instance_eval of a DSL, is the ultimate monkey patch
dsl = yar.eval_rjs('//not//used//yet', 'form.login = "Groo"')
old = yar.eval_rjs('//not//used//yet', 'rjs["login"].value = "Groo"')
assert_equal old.to_s, dsl.to_s
end
def test_eval_rjs
yar = YarWiki.new('NotUsed')
assert_javascript yar.eval_rjs('//not//used//yet', 'rjs.show "Daisy_Mae"')
assert_xpath :'Statement[1]//ArgumentList[1]' do
sauce = assert_js_argument(1)
sauce.gsub!('\\"', '"')
sauce.gsub!('\\\'', '\'')
assert_javascript sauce # ERGO better error message out of here!
end
end
def test_rjs_assert
assert_js get_rjs.assert('true')
assert_xpath :'Statement[1]/IfStatement//Statement[ @name = "true" ]' do
assert_js_call :report do
assert_match /failed/, assert_js_argument(1)
end
end
end
end
def test_rjs_surf # ERGO use this or lose it
rjs = get_rjs
js = rjs.surf('"thar"') do
rjs << 'alert("yo");'
end
return unless got_pure_perl?
assert_js js
# ERGO @xdoc.write($stdout, 2)
# assert_xpath 'Statement[1]/IfStatement//Statement[ @name = "true" ]' do
# assert_xpath './/Identifier[ @name = "Callee" and . = "report" ]'
#< assert_xpath './/ArgumentList[1]//String' do |string|
# assert_match /failed/, string.text
# end
# end
end
def test_rjs_assert_ajax
rjs = get_rjs()
rjs.assert_ajax(/BunBun/) {}
return unless got_pure_perl?
assert_js rjs.to_s
assert_xpath ('*[ . = "Ajax" ]/' +
'../*[ . = "Responders" ]/' +
'../../*[ . = "register" ]').to_sym
end
def test_rjs_assert_ajax_with_click
rjs = get_rjs()
rjs.assert_ajax /link_node.test_BunBun/ do
rjs.assert 'true'
end
return unless got_pure_perl?
assert_js rjs.to_s
return # FIXME this had a bug that REXML didn't catch!
assert_xpath :'Statement[1]/descendant::String [ . = "link_node:test_BunBun" ]'
assert_xpath :AnonymousFunctionDeclaration do
assert_js_call :report
end
end
def test_wiki
yar = YarWiki.new('WikiTestPage')
yar.save_page('!omap - Little: Nemo')
get :index, :id => yar.page_name
assert_select 'div#wiki_panel' do
assert_select 'strong', /Little/
assert_select 'li', /Nemo/
end
end
def reveal()
page_name = @yar ? @yar.page_name : 'WikiTestPage'
system("firefox http://localhost:3000/wiki/index/#{page_name}")
end
# disable these two because I don't care what you do with
# your URIs...
def _test_clean_uri #:nodoc:
assert_routing '/wiki/WikiTestPage',
:controller => 'wiki',
:action => 'index',
:id => ['WikiTestPage']
end
def _test_dont_route_actions #:nodoc:
assert_routing '/wiki/action',
:controller => 'wiki',
:action => 'action'
end
def render_wiki(source)
@yar = YarWiki.new('WikiTestPage')
@yar.save_page(source)
get :index, :id => @yar.page_name
assert_select 'div#wiki_panel' do |wiki_panel|
assert_xml wiki_panel
end
end
def test_render_nested_omaps # dried this old test up
render_wiki(get_omap)
assert_xpath '//div[ . = "/character/uncle_wiggily" ]'
end
# CONSIDER system to hilite how far an xpath gets
def test_update_yaml_node
@yar = YarWiki.new('WikiTestPage')
@yar.save_page(get_omap)
xhr :get, :edit_node,
:page_name => 'WikiTestPage',
:ypath => 'node:test_uncle_wiggily:script'
# puts @response.body
form = select_form '/wiki/update_yaml_node'
spike = 'new contents'
form['node_contents'] = spike
ypath = '//test_uncle_wiggily//script'
assert_equal ypath, form['ypath'].value
assert_equal @yar.page_name, form['page_name'].value
form.submit_without_clicking_button
yaml = YAML.parse_file(@yar.yaml_path)
assert_equal spike, yaml.select(ypath)[0].value
# reveal()
end
def test_render_nested_omaps # dried this old test up
render_wiki(get_omap)
assert_xpath :'div[ . = "/character/uncle_wiggily" ]'
end
def test_test_nodes_link_to_display_iframe
render_wiki(get_omap)
assert_xpath :'strong/a[ . = "test" ]' do |a|
assert_equal '#', a[:href]
return unless got_pure_perl?
assert_js 'ajax = ' + a[:onclick]
assert_xpath 'Statement[ 1 ]//ArgumentList[ 1 ]' do
command, params = assert_params(assert_js_argument(1))
assert{ '/wiki/display_iframe' == command }
assert{ '//test_uncle_wiggily' == params[:ypath] }
assert{ 'WikiTestPage' == params[:page_name] }
end
end
end
# ERGO count the assertions and pass the test when that many vote in.
def test_display_iframe
# ERGO better test isolation!
# ERGO TDA turn this off more
#:stopdoc:
ActionView::Base.debug_rjs = false
YarWiki.new('WikiTestPage').save_page(get_omap)
xhr :get, :display_iframe,
:page_name => 'WikiTestPage',
:ypath => '//test_hammy_squirrel'
# assert_rjs :page, 'wiki_panel', :width=, '50%'
# assert_rjs :page, 'test_panel', :width=, '50%'
assert_rjs :show, 'test_panel'
assert_rjs :page, 'bar_node:test_hammy_squirrel', :className=, 'wiki_green'
assert_rjs :replace_html, 'test_panel', /iframe.+character.+hammy/
#:startdoc:
end
def test_test_iframe
render_wiki(get_omap)
assert_xml @controller.send(:test_iframe, @yar, '//test_uncle_wiggily')
assert_xpath :'iframe[ @src = "/character/uncle_wiggily" ]'
end
def self_test
set_wiki_test_up
get :index, :id => @yar.page_name
assert_select 'div#wiki_panel' do |wiki_panel|
return wiki_panel
end
end
def test_wiki_yaml_action
@yar = YarWiki.new('WikiTestPage')
@yar.save_page(get_omap)
xhr :get, :edit_node,
:page_name => 'WikiTestPage',
:ypath => 'node:test_uncle_wiggily:script'
assert_xpath :'form' do
assert_xpath 'textarea[ @name = "node_contents" ]' do |node|
assert_match /tests uncle wiggily/, node.text
end
end
end
def test_onkeydown
@yar = YarWiki.new('WikiTestPage')
@yar.save_page(get_omap)
xhr :get, :edit_node,
:page_name => 'WikiTestPage',
:ypath => 'node:test_uncle_wiggily:script'
assert_xpath :'form/textarea' do |textarea|
return unless got_pure_perl?
assert_javascript textarea[:onkeydown]
assert_xpath :'Statement[1]' do assert_js_call :editor_keydown end
end
end
def test_iframe_onload
set_wiki_test_up
assert_xml @controller.send(:test_iframe, @yar, '//test_WikiTestPage')
assert_xpath :'iframe' do |iframe|
assert_match /link.node.test_uncle_wiggily/, iframe[:onload]
end
end
def test_rjs_trap_errors
set_wiki_test_up
ypath = '//test_ClickLink'
assertion = @yar.get_node(ypath + '//script')
assertion.value = 'radio (kozmik bullfrog) edit'
@yar.save_page
xhr :get, :display_iframe, # ERGO format all xhr like this
:page_name => @yar.page_name,
:ypath => ypath
assert_rjs :replace_html, :transcript, /compile error/
end
# ERGO onComplete() should return pure XML
def test_surf_self
wiki_panel = self_test
# assert_xml wiki_panel
# reveal()
end
end
end # if test mode