Skip to content

Commit

Permalink
#138 test passes
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Sep 25, 2024
1 parent 31af3c1 commit fb60b5a
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 13 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ gem 'fbe', '>0'
gem 'judges', '>0'
gem 'minitest', '5.25.1', require: false
gem 'minitest-reporters', '1.7.1', require: false
gem 'qbash', '0.0.3', require: false
gem 'rake', '13.2.1', require: false
gem 'redcarpet', '~>3.5'
gem 'rubocop', '1.66.1', require: false
Expand Down
24 changes: 14 additions & 10 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,15 @@ GEM
others (> 0)
tago (> 0)
yaml (~> 0.3)
faraday (2.10.1)
faraday-net_http (>= 2.0, < 3.2)
faraday (2.12.0)
faraday-net_http (>= 2.0, < 3.4)
json
logger
faraday-http-cache (2.5.1)
faraday (>= 0.8)
faraday-multipart (1.0.4)
multipart-post (~> 2)
faraday-net_http (3.1.1)
faraday-net_http (3.3.0)
net-http
faraday-retry (2.2.1)
faraday (~> 2.0)
Expand Down Expand Up @@ -151,18 +152,21 @@ GEM
sawyer (~> 0.9)
others (0.0.3)
parallel (1.26.3)
parser (3.3.4.2)
parser (3.3.5.0)
ast (~> 2.4.1)
racc
public_suffix (6.0.1)
qbash (0.0.3)
backtrace (> 0)
elapsed (> 0)
loog (> 0)
racc (1.8.1)
rainbow (3.1.1)
rake (13.2.1)
redcarpet (3.6.0)
regexp_parser (2.9.2)
retries (0.0.5)
rexml (3.3.6)
strscan
rexml (3.3.7)
rubocop (1.66.1)
json (~> 2.3)
language_server-protocol (>= 3.17.0)
Expand All @@ -173,7 +177,7 @@ GEM
rubocop-ast (>= 1.32.2, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.32.2)
rubocop-ast (1.32.3)
parser (>= 3.3.1.0)
ruby-progressbar (1.13.0)
sawyer (0.9.2)
Expand All @@ -187,15 +191,14 @@ GEM
simplecov-cobertura (2.1.0)
rexml
simplecov (~> 0.19)
simplecov-html (0.12.3)
simplecov-html (0.13.1)
simplecov_json_formatter (0.1.4)
strscan (3.1.0)
tago (0.0.2)
typhoeus (1.4.1)
ethon (>= 0.9.0)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
unicode-display_width (2.5.0)
unicode-display_width (2.6.0)
uri (0.13.1)
verbose (0.0.2)
loog (~> 0.2)
Expand Down Expand Up @@ -231,6 +234,7 @@ DEPENDENCIES
judges (> 0)
minitest (= 5.25.1)
minitest-reporters (= 1.7.1)
qbash (= 0.0.3)
rake (= 13.2.1)
redcarpet (~> 3.5)
rubocop (= 1.66.1)
Expand Down
101 changes: 101 additions & 0 deletions test/pages/test_awards.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# frozen_string_literal: true

# MIT License
#
# Copyright (c) 2024 Zerocracy
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

require 'minitest/autorun'
require 'nokogiri'
require 'qbash'
require 'loog'

# Test.
# Author:: Yegor Bugayenko ([email protected])
# Copyright:: Copyright (c) 2024 Yegor Bugayenko
# License:: MIT
class TestAwards < Minitest::Test
def test_payables
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='/'>
<xsl:copy-of select=\"z:payables('dude')\"/>
</xsl:template>
</xsl:stylesheet>
"
)
xml = File.join(dir, 'input.xml')
File.write(
xml,
"
<fb>
<f>
<what>reconciliation</what>
<when>#{(Time.now - (60 * 60)).utc.iso8601}</when>
<since>#{(Time.now - (50 * 60 * 60)).utc.iso8601}</since>
<who_name>dude</who_name>
<awarded>100</awarded>
<payout>70</payout>
<balance>30</balance>
</f>
<f>
<is_human>1</is_human>
<when>#{(Time.now - (10 * 60 * 60)).utc.iso8601}</when>
<who_name>dude</who_name>
<award>40</award>
</f>
<f>
<is_human>1</is_human>
<when>#{(Time.now - (10 * 60 * 60)).utc.iso8601}</when>
<who_name>dude</who_name>
<award>60</award>
</f>
<f>
<is_human>1</is_human>
<when>#{Time.now.utc.iso8601}</when>
<who_name>dude</who_name>
<award>25</award>
</f>
</fb>
"
)
output = File.join(dir, 'output.xml')
qbash(
[
"java -jar #{Shellwords.escape(File.join(__dir__, '../../target/saxon.jar'))}",
"-s:#{Shellwords.escape(xml)}",
"-xsl:#{Shellwords.escape(xsl)}",
"-o:#{Shellwords.escape(output)}",
"today=#{Shellwords.escape((Time.now + 10).utc.iso8601)}"
],
log: Loog::NULL
)
out = Nokogiri::XML.parse(File.read(output))
assert_equal('55', out.xpath('/td/text()').to_s, out)
end
end
end
25 changes: 22 additions & 3 deletions xsl/awards.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ SOFTWARE.
<xsl:variable name="rec" select="$fb/f[what='reconciliation' and who_name=$name][last()]"/>
<xsl:choose>
<xsl:when test="$rec">
<xsl:for-each select="'awarded', 'since', 'balance', 'payout', 'when'">
<xsl:variable name="n" select="."/>
<xsl:if test="not($rec/*[name()=$n])">
<xsl:message terminate="yes">
<xsl:text>There is no '</xsl:text>
<xsl:value-of select="."/>
<xsl:text>' property in the fact</xsl:text>
</xsl:message>
</xsl:if>
</xsl:for-each>
<td class="right ff">
<xsl:variable name="accumulated" select="sum($facts[who_name=$name and xs:dateTime(when) &gt; xs:dateTime($rec/since)]/award)"/>
<xsl:variable name="delta" select="$accumulated - xs:integer($rec/awarded)"/>
Expand All @@ -57,17 +67,26 @@ SOFTWARE.
<xsl:value-of select="xs:date(xs:dateTime($rec/when))"/>
<xsl:text>, making the amount payable equal to </xsl:text>
<xsl:value-of select="$rec/balance"/>
<xsl:text>; since then you've accumulated </xsl:text>
<xsl:text>; since </xsl:text>
<xsl:value-of select="xs:date(xs:dateTime($rec/since))"/>
<xsl:text> you've accumulated </xsl:text>
<xsl:value-of select="$delta"/>
<xsl:text> points, that's why the amount payable now is </xsl:text>
<xsl:text> points (</xsl:text>
<xsl:value-of select="$accumulated"/>
<xsl:text> - </xsl:text>
<xsl:value-of select="xs:integer($rec/awarded)"/>
<xsl:text>), that's why the amount payable now is </xsl:text>
<xsl:value-of select="$payable"/>
</xsl:attribute>
<xsl:value-of select="$payable"/>
</td>
</xsl:when>
<xsl:otherwise>
<td>
<xsl:text> </xsl:text>
<xsl:comment>
<xsl:text>There was no reconciliation (payouts) as of yet. </xsl:text>
<xsl:text>The entire rolling balance may be paid.</xsl:text>
</xsl:comment>
</td>
</xsl:otherwise>
</xsl:choose>
Expand Down

0 comments on commit fb60b5a

Please sign in to comment.