# encoding: UTF-8
require 'spec_helper'
describe TmxParser do
let(:parser) { TmxParser }
let(:tuid) { '79b371014a8382a3b6efb86ec6ea97d9' }
def find_variant(locale, unit)
unit.variants.find { |v| v.locale == locale }
end
context 'with a basic tmx document' do
let(:document) do
%Q{
0
six.hours
6 hours
6 Stunden
}
end
it 'identifies the tuid and segtype' do
parser.load(document).to_a.tap do |units|
expect(units.size).to eq(1)
units.first.tap do |unit|
expect(unit.tuid).to eq(tuid)
expect(unit.segtype).to eq('block')
end
end
end
it 'identifies the correct variants' do
parser.load(document).to_a.first.tap do |unit|
expect(unit.variants.size).to eq(2)
expect(find_variant('en-US', unit).elements).to eq(['6 hours'])
expect(find_variant('de-DE', unit).elements).to eq(['6 Stunden'])
unit.variants.each do |variant|
expect(variant).to be_a(TmxParser::Variant)
end
end
end
it 'identifies properties' do
parser.load(document).to_a.first.tap do |unit|
expect(unit.properties.size).to eq(2)
expect(unit.properties).to include('x-segment-id')
expect(unit.properties).to include('x-some-property')
expect(unit.properties['x-segment-id'].value).to eq('0')
expect(unit.properties['x-some-property'].value).to eq('six.hours')
end
end
end
context 'with a tmx document that contains a property that makes jruby cry' do
# For some reason, jruby doesn't like square brackets in property values.
# See: https://github.com/sparklemotion/nokogiri/issues/1261
let(:document) do
%Q{
0
en:#:daily-data:#:[3]:#:times
6 hours
6 Stunden
}
end
it 'identifies the property correctly' do
parser.load(document).to_a.first.tap do |unit|
expect(unit.properties).to include('x-some-property')
expect(unit.properties['x-some-property']).to be_a(TmxParser::PropertyValue)
expect(unit.properties['x-some-property'].value).to eq(
'en:#:daily-data:#:[3]:#:times'
)
end
end
end
context 'with a tmx document that contains placeholders' do
let(:document) do
%Q{
0
{0} sessions
{0} Einheiten
}
end
it 'identifies the placeholders' do
parser.load(document).to_a.first.tap do |unit|
expect(unit.variants.size).to eq(2)
find_variant('en-US', unit).tap do |en_variant|
expect(en_variant.elements.size).to eq(2)
en_variant.elements.first.tap do |first_element|
expect(first_element.type).to eq('x-placeholder')
expect(first_element.text).to eq('{0}')
end
en_variant.elements.last.tap do |last_element|
expect(last_element).to be_a(String)
expect(last_element).to eq(' sessions')
end
end
find_variant('de-DE', unit).tap do |en_variant|
expect(en_variant.elements.size).to eq(2)
en_variant.elements.first.tap do |first_element|
expect(first_element).to be_a(TmxParser::Placeholder)
expect(first_element.type).to eq('x-placeholder')
expect(first_element.text).to eq('{0}')
end
en_variant.elements.last.tap do |last_element|
expect(last_element).to be_a(String)
expect(last_element).to eq(' Einheiten')
end
end
end
end
end
context 'with a tmx document that contains paired tags' do
let(:document) do
%Q{
0
Build your healthy habit of daily training with <strong>email training reminders.</strong>
<strong>Mit Erinnerungen per E-Mail</strong> können Sie das tägliche Training zu einer schönen Angewohnheit werden lassen.
}
end
it 'identifies the tags' do
parser.load(document).to_a.first.tap do |unit|
expect(unit.variants.size).to eq(2)
find_variant('en-US', unit).tap do |en_variant|
expect(en_variant.elements.size).to eq(4)
en_variant.elements[0].tap do |element|
expect(element).to be_a(String)
expect(element).to eq('Build your healthy habit of daily training with ')
end
en_variant.elements[1].tap do |element|
expect(element).to be_a(TmxParser::BeginPair)
expect(element.i).to eq('3')
expect(element.text).to eq('')
end
en_variant.elements[2].tap do |element|
expect(element).to be_a(String)
expect(element).to eq('email training reminders.')
end
en_variant.elements[3].tap do |element|
expect(element).to be_a(TmxParser::EndPair)
expect(element.i).to eq('3')
expect(element.text).to eq('')
end
end
end
end
end
end