Skip to content

Commit

Permalink
#138 more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Sep 26, 2024
1 parent 97d2d46 commit 927bafb
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 41 deletions.
44 changes: 6 additions & 38 deletions test/pages/test_awards.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
# Copyright:: Copyright (c) 2024 Yegor Bugayenko
# License:: MIT
class TestAwards < Minitest::Test
def test_payables
def test_fn_payables
xml = xslt(
"<xsl:copy-of select=\"z:payables('dude')\"/>",
"
Expand Down Expand Up @@ -71,7 +71,7 @@ def test_payables
assert_equal('55', xml.xpath('/td/text()').to_s, xml)
end

def test_payables_out_of_window
def test_fn_payables_out_of_window
xml = xslt(
"<xsl:copy-of select=\"z:payables('dude')\"/>",
"
Expand Down Expand Up @@ -104,7 +104,7 @@ def test_payables_out_of_window
assert_equal('75', xml.xpath('/td/text()').to_s, xml)
end

def test_payables_with_few_reconciliations
def test_fn_payables_with_few_reconciliations
xml = xslt(
"<xsl:copy-of select=\"z:payables('dude')\"/>",
"
Expand Down Expand Up @@ -152,7 +152,7 @@ def test_payables_with_few_reconciliations
assert_equal('55', xml.xpath('/td/text()').to_s, xml)
end

def test_monday
def test_fn_monday
xml = xslt(
'<r><xsl:value-of select="z:monday(1)"/></r>',
'
Expand All @@ -169,7 +169,7 @@ def test_monday
assert_equal('2024-09-23Z', xml.xpath('/r/text()').to_s, xml)
end

def test_in_week
def test_fn_in_week
xml = xslt(
'<r><xsl:value-of select="z:in-week(\'2024-09-20T04:04:04Z\', 1)"/></r>',
'
Expand All @@ -186,7 +186,7 @@ def test_in_week
assert_equal('true', xml.xpath('/r/text()').to_s, xml)
end

def test_award
def test_fn_award
{
42 => ['darkgreen', '+42'],
0 => ['lightgray', '&#x2014;'],
Expand All @@ -200,36 +200,4 @@ def test_award
assert_equal(v[1], xml.xpath('/span/text()').to_s, xml)
end
end

private

def xslt(template, xml, vars = {})
Dir.mktmpdir do |dir|
xsl = File.join(dir, 'foo.xsl')
File.write(
xsl,
"
<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
xmlns:xs='http://www.w3.org/2001/XMLSchema' xmlns:z='https://www.zerocracy.com'
version='2.0' exclude-result-prefixes='xs z'>
<xsl:import href='#{File.join(__dir__, '../../xsl/vitals.xsl')}'/>
<xsl:template match='/'>#{template}</xsl:template>
</xsl:stylesheet>
"
)
input = File.join(dir, 'input.xml')
File.write(input, xml)
output = File.join(dir, 'output.xml')
qbash(
[
"java -jar #{Shellwords.escape(File.join(__dir__, '../../target/saxon.jar'))}",
"-s:#{Shellwords.escape(input)}",
"-xsl:#{Shellwords.escape(xsl)}",
"-o:#{Shellwords.escape(output)}"
] + vars.map { |k, v| Shellwords.escape("#{k}=#{v}") },
log: Loog::NULL
)
Nokogiri::XML.parse(File.read(output))
end
end
end
32 changes: 32 additions & 0 deletions test/pages/test_vitals.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
require 'webmock/minitest'
require 'nokogiri'
require 'w3c_validators'
require_relative '../test__helper'

# Test.
# Author:: Yegor Bugayenko ([email protected])
Expand All @@ -52,4 +53,35 @@ def test_validate_html
# v = W3CValidators::NuValidator.new.validate_file(html)
# assert(v.errors.empty?, "#{doc}\n\n#{v.errors.join('; ')}")
end

def test_fn_index
{
3.3 => ['darkgreen', '+3.30'],
0 => ['darkgreen', '+0.00'],
-1 => ['darkred', '-1.00']
}.each do |k, v|
xml = xslt(
"<xsl:copy-of select='z:index(#{k})'/>",
'<fb/>'
)
assert_equal(v[0], xml.xpath('/span/@class').to_s, xml)
assert_equal(v[1], xml.xpath('/span/text()').to_s, xml)
end
end

def test_fn_pmp
xml = xslt(
'<r><xsl:value-of select="z:pmp(\'hr\', \'foo\', \'bar\')"/></r>',
'
<fb>
<f>
<what>pmp</what>
<area>foo</area>
<xyz>test</xyz>
</f>
</fb>
'
)
assert_equal('bar', xml.xpath('/r/text()').to_s, xml)
end
end
35 changes: 35 additions & 0 deletions test/test__helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@
require 'minitest/reporters'
Minitest::Reporters.use! [Minitest::Reporters::SpecReporter.new]

require 'judges/options'
require 'loog'
require 'minitest/autorun'
require 'nokogiri'
require 'qbash'
require 'shellwords'

class Minitest::Test
def load_it(judge, fb)
Expand All @@ -49,4 +54,34 @@ def load_it(judge, fb)
def stub_github(url, body:, method: :get, status: 200, headers: { 'content-type': 'application/json' })
stub_request(method, url).to_return(status:, body: body.to_json, headers:)
end

def xslt(template, xml, vars = {})
Dir.mktmpdir do |dir|
xsl = File.join(dir, 'foo.xsl')
File.write(
xsl,
"
<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
xmlns:xs='http://www.w3.org/2001/XMLSchema' xmlns:z='https://www.zerocracy.com'
version='2.0' exclude-result-prefixes='xs z'>
<xsl:import href='#{File.join(__dir__, '../xsl/vitals.xsl')}'/>
<xsl:template match='/'>#{template}</xsl:template>
</xsl:stylesheet>
"
)
input = File.join(dir, 'input.xml')
File.write(input, xml)
output = File.join(dir, 'output.xml')
qbash(
[
"java -jar #{Shellwords.escape(File.join(__dir__, '../target/saxon.jar'))}",
"-s:#{Shellwords.escape(input)}",
"-xsl:#{Shellwords.escape(xsl)}",
"-o:#{Shellwords.escape(output)}"
] + vars.map { |k, v| Shellwords.escape("#{k}=#{v}") },
log: Loog::NULL
)
Nokogiri::XML.parse(File.read(output))
end
end
end
3 changes: 1 addition & 2 deletions xsl/awards.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:z="https://www.zerocracy.com" version="2.0" exclude-result-prefixes="xs z">
<xsl:variable name="fb" select="/fb"/>
<xsl:variable name="days" select="z:pmp(/fb, 'hr', 'days_of_running_balance', '28')"/>
<xsl:variable name="days" select="z:pmp('hr', 'days_of_running_balance', '28')"/>
<xsl:variable name="weeks" select="xs:integer(ceiling(xs:float($days) div 7))"/>
<xsl:variable name="since" select="xs:dateTime($today) - xs:dayTimeDuration(concat('P', $days, 'D'))"/>
<xsl:variable name="facts" select="$fb/f[award and xs:dateTime(when) &gt; $since and is_human = 1]"/>
Expand Down
12 changes: 11 additions & 1 deletion xsl/vitals.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ SOFTWARE.
<xsl:import href="bylaws.xsl"/>
<xsl:import href="qo-section.xsl"/>
<xsl:import href="dot.xsl"/>
<xsl:variable name="fb" select="/fb"/>
<xsl:function name="z:index">
<!--
Converts a number to a "span" with a properly formatted index value.
The span will have a "class" with the HTML color, according to the value.
-->
<xsl:param name="i" as="xs:double"/>
<span>
<xsl:attribute name="class">
Expand All @@ -61,7 +66,12 @@ SOFTWARE.
</span>
</xsl:function>
<xsl:function name="z:pmp">
<xsl:param name="fb"/>
<!--
Finds a "pmp" fact with the given "area" and then
tries to find a given property inside. If the fact is not
found or the property doesn't exists, the default value
is returned.
-->
<xsl:param name="area" as="xs:string"/>
<xsl:param name="param" as="xs:string"/>
<xsl:param name="default" as="xs:string"/>
Expand Down

0 comments on commit 927bafb

Please sign in to comment.