-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathmodel_literal_spec.rb
2516 lines (2327 loc) · 100 KB
/
model_literal_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# coding: utf-8
# frozen_string_literal: true
require_relative 'spec_helper'
require 'rdf/spec/literal'
require 'rdf/xsd'
describe RDF::Literal do
XSD = RDF::XSD
def self.literal(selector)
case selector
when :empty then ['']
when :plain then ['Hello']
when :empty_lang then ['', {language: :en}]
when :plain_lang then ['Hello', {language: :en}]
# langString language: must not contain spaces
when :wrong_lang then ['WrongLang', {language: "en f"}]
# langString language: must be non-empty valid language
when :unset_lang then ['NoLanguage', {datatype: RDF.langString}]
when :lang_dir then ['Hello', {language: :en, direction: :ltr}]
when :wrong_dir then ['Hello', {language: :en, direction: "center-out"}]
when :dir_no_lang then ['Hello', {direction: :ltr}]
when :unset_dir then ['NoDir', {language: :en, datatype: RDF.dirLangString}]
when :string then ['String', {datatype: RDF::XSD.string}]
when :false then [false]
when :true then [true]
when :int then [123]
when :long then [9223372036854775807]
when :double then [3.1415]
when :decimal then [BigDecimal("1.2")]
when :date then [Date.new(2010)]
when :datetime then [DateTime.new(2011)]
when :time then ['01:02:03Z', {datatype: RDF::XSD.time}]
else
raise("unexpected literal: :#{selector}")
end
end
def self.literals(*selector)
selector.inject([]) do |ary, sel|
ary += case sel
when :all_simple then %i(empty plain string).map {|s| literal(s)}
when :all_plain_lang then %i(empty_lang plain_lang lang_dir).map {|s| literal(s)}
when :all_native then %i(false true int long decimal double time date datetime).map {|s| literal(s)}
when :all_invalid_lang then %i(wrong_lang unset_lang wrong_dir).map {|s| literal(s)}
when :all_plain then literals(:all_simple, :all_plain_lang)
else literals(:all_plain, :all_native)
end
end
end
describe "new" do
it "instantiates empty string" do
expect { RDF::Literal.new('') }.not_to raise_error
end
it "instantiates empty string with language" do
expect { RDF::Literal.new('', language: :en) }.not_to raise_error
end
it "instantiates from native datatype" do
expect { RDF::Literal.new(123) }.not_to raise_error
end
it "encodes as utf-8" do
ascii = "foo".encode(Encoding::ASCII)
expect(RDF::Literal.new(ascii).to_s.encoding).to eq Encoding::UTF_8
end
describe "c18n" do
it "normalizes language to lower-case" do
expect(RDF::Literal.new('Upper', language: :EN, canonicalize: true).language).to eq :en
end
it "supports sub-taged language specification" do
expect(RDF::Literal.new('Hi', language: :"en-us", canonicalize: true).language).to eq :"en-us"
end
# Native representations
[Date.today, Time.now, DateTime.now].each do |v|
it "creates a valid literal from #{v.inspect}" do
expect(RDF::Literal(v, canonicalize: true)).to be_valid
end
end
end
end
describe "#plain?" do
literals(:all_plain).each do |args|
it "returns true for #{args.inspect}" do
options = args.last.is_a?(Hash) ? args.pop : {}
expect(RDF::Literal.new(*args, **options)).to be_plain
end
end
(literals(:all) - literals(:all_plain)).each do |args|
it "returns false for #{args.inspect}" do
options = args.last.is_a?(Hash) ? args.pop : {}
expect(RDF::Literal.new(*args, **options)).not_to be_plain
end
end
end
describe "#simple?" do
literals(:all_simple).each do |args|
it "returns true for #{args.inspect}" do
options = args.last.is_a?(Hash) ? args.pop : {}
expect(RDF::Literal.new(*args, **options)).to be_simple
end
end
(literals(:all) - literals(:all_simple)).each do |args|
it "returns false for #{args.inspect}" do
options = args.last.is_a?(Hash) ? args.pop : {}
expect(RDF::Literal.new(*args, **options)).not_to be_simple
end
end
end
describe "#language" do
literals(:all_plain_lang).each do |args|
it "returns language for #{args.inspect}" do
options = args.last.is_a?(Hash) ? args.pop : {}
expect(RDF::Literal.new(*args, **options).language).to eq :en
end
end
(literals(:all) - literals(:all_plain_lang)).each do |args|
it "returns nil for #{args.inspect}" do
options = args.last.is_a?(Hash) ? args.pop : {}
expect(RDF::Literal.new(*args, **options).language).to be_nil
end
end
end
describe "#english?" do
literals(:all).each do |args|
options = args.last.is_a?(Hash) ? args.pop : {}
lit = RDF::Literal.new(*args, **options)
if lit.language? && lit.language.to_s.downcase.start_with?('en')
it "returns true for #{lit.inspect}" do
expect(lit).to be_english
end
else
it "returns false for #{lit.inspect}" do
expect(lit).not_to be_english
end
end
end
end
describe "#datatype" do
literals(:all_simple).each do |args|
it "returns xsd:string for #{args.inspect}" do
options = args.last.is_a?(Hash) ? args.pop : {}
literal = RDF::Literal.new(*args, **options)
expect(literal.datatype).to eq RDF::XSD.string
end
end
{
123 => "integer",
true => "boolean",
false => "boolean",
9223372036854775807 => "integer",
3.1415 => "double",
BigDecimal("1.2") => "decimal",
Date.new(2010) => "date",
DateTime.new(2011) => "dateTime",
Rational("1/3") => "double",
Time.parse("01:02:03Z") => "dateTime"
}.each_pair do |value, type|
it "returns xsd.#{type} for #{value.inspect} #{value.class}" do
expect(RDF::Literal.new(value).datatype).to eq XSD[type]
end
end
end
describe "#typed?" do
literals(:all_simple, :all_plain_lang).each do |args|
it "returns false for #{args.inspect}" do
options = args.last.is_a?(Hash) ? args.pop : {}
expect(RDF::Literal.new(*args, **options)).not_to be_typed
end
end
(literals(:all) - literals(:all_simple, :all_plain_lang)).each do |args|
it "returns true for #{args.inspect}" do
options = args.last.is_a?(Hash) ? args.pop : {}
expect(RDF::Literal.new(*args, **options)).to be_typed
end
end
end
it "#start_with?" do
expect(RDF::Literal('foo')).to be_start_with('foo')
expect(RDF::Literal('bar')).not_to be_start_with('foo')
expect(RDF::Literal('foo')).to be_start_with('foo', 'nope')
end
describe "#==" do
literals(:all_plain).each do |args|
it "returns true for #{args.inspect}" do
options = args.last.is_a?(Hash) ? args.pop : {}
expect(RDF::Literal.new(*args, **options)).to eq RDF::Literal.new(*args, **options)
end
end
literals(:all_simple).each do |args|
it "returns true for value of #{args.inspect}" do
options = args.last.is_a?(Hash) ? args.pop : {}
expect(RDF::Literal.new(*args, **options)).to eq RDF::Literal.new(*args, **options).value
end
end
literals(:all_plain_lang).each do |args|
it "returns false for value of #{args.inspect}" do
options = args.last.is_a?(Hash) ? args.pop : {}
expect(RDF::Literal.new(*args, **options)).not_to eq RDF::Literal.new(*args, **options).value
end
end
literals(:all_native).each do |args|
it "returns true for #{args.inspect}" do
options = args.last.is_a?(Hash) ? args.pop : {}
expect(RDF::Literal.new(*args, **options)).to eq RDF::Literal.new(*args, **options)
end
it "returns true for value of #{args.inspect}" do
#literal = RDF::Literal.new(*args)
#expect(literal).to eq literal.value # FIXME: fails on xsd:date, xsd:time, and xsd:dateTime
end
end
it "returns true for language taged literals differring in case" do
l1 = RDF::Literal.new("foo", language: :en)
l2 = RDF::Literal.new("foo", language: :EN)
expect(l1).to eq l2
end
end
describe "#to_s" do
literals(:all_plain).each do |args|
it "returns value for #{args.inspect}" do
options = args.last.is_a?(Hash) ? args.pop : {}
literal = RDF::Literal.new(*args, **options)
expect(literal.to_s).to eql(literal.value)
end
end
{
literal(:int) => "123",
literal(:true) => "true",
literal(:false) => "false",
literal(:long) => "9223372036854775807",
literal(:double) => "3.1415",
literal(:date) => "2010-01-01",
literal(:datetime) => "2011-01-01T00:00:00Z",
literal(:time) => "01:02:03Z"
}.each_pair do |args, rep|
it "returns #{rep} for #{args.inspect}" do
options = args.last.is_a?(Hash) ? args.pop : {}
literal = RDF::Literal.new(*args, **options)
expect(literal.to_s).to eql(rep)
end
end
end
# to_str is implemented for stringy xsd types, but not others
describe "#to_str" do
literals(:all_plain).each do |args|
it "is implemented for #{args.inspect}" do
options = args.last.is_a?(Hash) ? args.pop : {}
expect(RDF::Literal.new(*args, **options)).to respond_to :to_str
end
it "matches #to_s for #{args.inspect}" do
options = args.last.is_a?(Hash) ? args.pop : {}
literal = RDF::Literal.new(*args, **options)
expect(literal.to_str).to eq literal.to_s
end
end
(literals(:all) - literals(:all_plain)).each do |args|
options = args.last.is_a?(Hash) ? args.pop : {}
it "is not implemented for #{args.inspect}" do
expect(RDF::Literal.new(*args, **options)).not_to respond_to :to_str
end
it "raises NoMethodError for #{args.inspect}" do
expect { RDF::Literal.new(*args, **options).to_str }.to raise_error NoMethodError
end
end
end
describe "#object" do
literals(:all_plain).each do |args|
it "returns value for #{args.inspect}" do
options = args.last.is_a?(Hash) ? args.pop : {}
literal = RDF::Literal.new(*args, **options)
expect(literal.object).to eql(literal.value)
end
end
{
literal(:int) => 123,
literal(:true) => true,
literal(:false) => false,
literal(:long) => 9223372036854775807,
literal(:double) => 3.1415,
literal(:date) => ::DateTime.new(2010),
literal(:datetime) => ::DateTime.new(2011),
# This is problematic when date changes between local testing location and UTC
#literal(:time) => ::DateTime.parse('01:02:03Z')
}.each_pair do |args, value|
it "returns object for #{args.inspect}" do
options = args.last.is_a?(Hash) ? args.pop : {}
literal = RDF::Literal.new(*args, **options)
expect(literal.object).to eql(value)
end
end
end
describe "#anonymous?" do
it "returns false" do
expect(RDF::Literal.new("")).not_to be_anonymous
end
end
describe "#compatible?" do
{
%("abc") => {
%("b") => true,
%("b^^<http://www.w3.org/2001/XMLSchema#string>") => true,
%("b"@ja) => false,
%("b"@en) => false,
%(<a>) => false,
%(_:a) => false,
},
%("abc^^<http://www.w3.org/2001/XMLSchema#string>") => {
%("b") => true,
%("b^^<http://www.w3.org/2001/XMLSchema#string>") => true,
%("b"@en) => false
},
%("abc"@en) => {
%("b") => true,
%("b^^<http://www.w3.org/2001/XMLSchema#string>") => true,
%("b"@en) => true,
%("b"@ja) => false
},
%("abc"@fr) => {
%("b"@ja) => false
},
}.each do |l1, props|
props.each do |l2, res|
it "#{l1} should #{'not ' unless res}be compatible with #{l2}" do
if res
expect(RDF::NTriples::Reader.parse_object l1).to be_compatible(RDF::NTriples::Reader.parse_object l2)
else
expect(RDF::NTriples::Reader.parse_object l1).not_to be_compatible(RDF::NTriples::Reader.parse_object l2)
end
end
end
end
end
describe "#squish" do
let(:result) {"a b c"}
subject {RDF::Literal(" a\n b c\n ")}
it "squeezes properly" do
expect(subject.squish).to eq result
end
it "returns a new object" do
expect(subject.squish).not_to equal subject
end
end
describe "#squish!" do
let(:result) {"a b c"}
subject {RDF::Literal(" a\n b c\n ")}
it "squeezes properly" do
expect(subject.squish!).to eq result
end
it "returns itself" do
expect(subject.squish!).to equal subject
end
end
describe "language-tagged string" do
literals(:all_plain_lang).each do |args|
it "validates #{args.inspect}" do
options = args.last.is_a?(Hash) ? args.pop : {}
expect(RDF::Literal.new(*args, **options)).to be_valid
end
end
literals(:all_invalid_lang).each do |args|
it "invalidates #{args.inspect}" do
options = args.last.is_a?(Hash) ? args.pop : {}
expect(RDF::Literal.new(*args, **options)).not_to be_valid
end
end
# test to make sure extra validation is not needed
context "when language? && !@language" do
langString = RDF::Literal.new("hello", datatype: RDF::langString)
it "should be invalid" do
expect(langString.language?).to be true
expect(!langString.instance_variable_get("@language")).to be true
expect(langString).not_to be_valid
end
end
end
describe "datatyped literal" do
(literals(:all) - literals(:all_simple, :all_plain_lang) +
[["foo", datatype: RDF::URI("http://example/bar")]]).each do |args|
it "validates #{args.inspect}" do
options = args.last.is_a?(Hash) ? args.pop : {}
expect(RDF::Literal.new(*args, **options)).to be_valid
end
end
it "invalidates ['foo', datatype: 'bar']" do
expect(RDF::Literal.new("foo", datatype: "bar")).not_to be_valid
end
end
describe RDF::Literal::Boolean do
it_behaves_like 'RDF::Literal with datatype and grammar', "true", RDF::XSD.boolean
it_behaves_like 'RDF::Literal equality', "true", true
it_behaves_like 'RDF::Literal lexical values', "true"
it_behaves_like 'RDF::Literal canonicalization', RDF::XSD.boolean, [
%w(true true),
%w(false false),
%w(tRuE true),
%w(FaLsE false),
%w(1 true),
%w(0 false)
]
it_behaves_like 'RDF::Literal validation', RDF::XSD.boolean,
%w(true false 1 0),
%w(foo 10) + ['true false', 'true foo', 'tRuE' 'FaLsE']
context "object values" do
{
1 => ["true", "true"],
0 => ["false", "false"],
true => ["true", "true"],
false => ["false", "false"]
}.each do |obj, (str, canon)|
it "to_str #{obj} to #{str.inspect}" do
expect(described_class.new(obj).to_s).to eql str
end
it "canonicalizes #{obj} to #{canon.inspect}" do
expect(described_class.new(obj, canonicalize: true).to_s).to eql canon
end
end
end
end
describe RDF::Literal::Integer do
it_behaves_like 'RDF::Literal with datatype and grammar', "1", RDF::XSD.integer
it_behaves_like 'RDF::Literal equality', "1", 1
it_behaves_like 'RDF::Literal lexical values', "1"
it_behaves_like 'RDF::Literal canonicalization', RDF::XSD.integer, [
%w(01 1),
%w(0123 123),
%w(1 1),
%w(-1 -1),
%w(+1 1)
]
it_behaves_like 'RDF::Literal validation', RDF::XSD.integer,
%w(1 10 100 01 +1 -1),
%w(foo 10.1 12xyz) + ["1 2", "foo 1", "1 foo"]
context "object values" do
{
1 => ["1", "1"],
0 => ["0", "0"]
}.each do |obj, (str, canon)|
it "to_str #{obj} to #{str.inspect}" do
expect(described_class.new(obj).to_s).to eql str
end
it "canonicalizes #{obj} to #{canon.inspect}" do
expect(described_class.new(obj, canonicalize: true).to_s).to eql canon
end
end
end
describe "#**", skip: (RUBY_ENGINE == "jruby") do
{
"2^3": [2, 3, 8],
"-2^3": [-2, 3, -8],
"2^-3": [2, -3, 0.125e0],
"-2^-3": [-2, -3, -0.125e0],
"2^0": [2, 0, 1],
"0^0": [0, 0, 1],
"0^4": [0, 4, 0],
"0^-4": [0, -4, RDF::Literal::Double.new('INF')],
"16^0.5e0": [16, 0.5e0, 4.0e0],
"16^0.25e0": [16, 0.25e0, 2.0e0],
"-1^INF": [-1, RDF::Literal::Double.new('INF'), 1.0e0],
"-1^-INF": [-1, RDF::Literal::Double.new('-INF'), 1.0e0],
"1^INF": [1, RDF::Literal::Double.new('INF'), 1.0e0],
"1^-INF": [1, RDF::Literal::Double.new('-INF'), 1.0e0],
"1^-NaN": [1, RDF::Literal::Double.new('NaN'), 1.0e0],
}.each do |name, (n, e, result)|
it name do
expect(RDF::Literal(n) ** RDF::Literal(e)).to eq RDF::Literal(result)
expect((RDF::Literal(n) ** RDF::Literal(e)).datatype).to eq RDF::Literal(result).datatype
end
end
end
describe "#%" do
{
"10 % 3": [10, 3, 1],
"6 % -2": [6, -2, 0],
}.each do |name, (n, e, result)|
it name do
expect(RDF::Literal(n) % RDF::Literal(e)).to eq RDF::Literal(result)
end
end
end
end
describe RDF::Literal::Decimal do
it_behaves_like 'RDF::Literal with datatype and grammar', "1.1", RDF::XSD.decimal
it_behaves_like 'RDF::Literal equality', "1.1", 1.1
it_behaves_like 'RDF::Literal lexical values', "1.1"
it_behaves_like 'RDF::Literal canonicalization', RDF::XSD.decimal, [
%w(1 1.0),
%w(01 1.0),
%w(0123 123.0),
%w(-1 -1.0),
%w(1. 1.0),
%w(1.0 1.0),
%w(1.00 1.0),
%w(+001.00 1.0),
%w(123.456 123.456),
%w(0123.456 123.456),
%w(1.000000000 1.0),
%w(2.345 2.345),
%w(2.3 2.3),
%w(2.234000005 2.234000005),
%w(2.2340000000000005 2.2340000000000005),
%w(2.23400000000000005 2.234),
%w(2.23400000000000000000005 2.234),
%w(1.2345678901234567890123457890 1.2345678901234567),
]
it_behaves_like 'RDF::Literal validation', RDF::XSD.decimal,
%w(
1
-1
1.
1.0
1.00
+001.00
123.456
1.000000000
2.345
2.3
2.234000005
2.2340000000000005
2.23400000000000005
2.23400000000000000000005
1.2345678901234567890123457890
),
%w(foo 10.1e1 12.xyz) + ['1.0 foo', 'foo 1.0']
context "object values" do
{
BigDecimal('1.0') => ["1.0", "1.0"],
BigDecimal('0') => ["0.0", "0.0"],
BigDecimal('10.10') => ["10.1", "10.1"],
1.1 => ["1.1", "1.1"],
}.each do |obj, (str, canon)|
it "to_str #{obj} to #{str.inspect}" do
expect(described_class.new(obj).to_s).to eql str
end
it "canonicalizes #{obj} to #{canon.inspect}" do
expect(described_class.new(obj, canonicalize: true).to_s).to eql canon
end
end
end
describe "#%" do
{
"4.5 % 1.2": [BigDecimal("4.5"), BigDecimal("1.2"), BigDecimal("0.9")],
}.each do |name, (n, e, result)|
it name do
expect(RDF::Literal(n) % RDF::Literal(e)).to eq RDF::Literal(result)
end
end
end
describe "#**" do
{
"2.7^10": [BigDecimal("2.7"), 10, BigDecimal("20589.1132094649")],
"2.7^10.3": [BigDecimal("2.7"), BigDecimal("10.3"), BigDecimal("27736.1879020809118")],
}.each do |name, (n, e, result)|
it name do
value = RDF::Literal(n) ** RDF::Literal(e)
expect(value).to be_within(0.00001).of(result)
expect(value.datatype).to eq RDF::Literal(result).datatype
end
end
end
end
describe RDF::Literal::Double do
it_behaves_like 'RDF::Literal with datatype and grammar', "1.0E0", RDF::XSD.double
it_behaves_like 'RDF::Literal equality', "1.0E0", 1.0E0
it_behaves_like 'RDF::Literal lexical values', "1.0E0"
it_behaves_like 'RDF::Literal canonicalization', RDF::XSD.double, [
%w(1 1.0E0),
%w(01 1.0E0),
%w(0123 1.23E2),
%w(-1 -1.0E0),
%w(+01.000 1.0E0),
#%w(1. 1.0E0),
%w(1.0 1.0E0),
%w(123.456 1.23456E2),
%w(1.0e+1 1.0E1),
%w(1.0e-10 1.0E-10),
%w(123.456e4 1.23456E6),
%w(1.E-8 1.0E-8),
%w(+INF INF),
%w(INF INF),
%w(-INF -INF),
%w(+INF INF),
#%w(NaN NaN),
#%w(NAN NaN),
%w(InF INF),
%w(3E1 3.0E1)
]
it_behaves_like 'RDF::Literal validation', RDF::XSD.double,
%w(
1
-1
+01.000
1.0
123.456
1.0e+1
1.0e-10
123.456e4
INF
-INF
NaN
3E1
),
%w(foo 12.xyz 1.0ez) + ['1.1e1 foo', 'foo 1.1e1']
context "object values" do
{
1.0 => ["1.0", "1.0E0"],
0.0 => ["0.0", "0.0E0"],
10.10 => ["10.1", "1.01E1"],
123.456e4 => ["1234560.0", "1.23456E6"]
}.each do |obj, (str, canon)|
it "to_str #{obj} to #{str.inspect}" do
expect(described_class.new(obj).to_s).to eql str
end
it "canonicalizes #{obj} to #{canon.inspect}" do
expect(described_class.new(obj, canonicalize: true).to_s).to eql canon
end
end
end
let(:nan) {described_class.new("NaN")}
let(:inf) {described_class.new("INF")}
it "recognizes INF" do
expect(inf).to be_infinite
expect(RDF::Literal.new('INF', datatype: RDF::Literal::Double::DATATYPE)).to eq inf
expect {inf.canonicalize}.not_to raise_error
end
it "recognizes -INF" do
expect(-inf).to be_infinite
expect(RDF::Literal.new('-INF', datatype: RDF::Literal::Double::DATATYPE)).to eq(-inf)
expect {-inf.canonicalize}.not_to raise_error
end
it "recognizes NaN" do
expect(nan).to be_nan
expect(RDF::Literal.new('NaN', datatype: RDF::Literal::Double::DATATYPE)).to be_nan
expect {nan.canonicalize}.not_to raise_error
end
[-1, 0, 1].map {|n| described_class.new(n)}.each do |n|
{
:"+" => [described_class.new("INF"), described_class.new("INF"), described_class.new("-INF"), described_class.new("-INF")],
:"-" => [described_class.new("INF"), described_class.new("-INF"), described_class.new("-INF"), described_class.new("INF")],
}.each do |op, (lp, rp, lm, rm)|
it "returns #{lp} for INF #{op} #{n}" do
expect(inf.send(op, n)).to eq lp
end
it "returns #{rp} for #{n} #{op} INF" do
expect(n.send(op, inf)).to eq rp
end
it "returns #{lm} for -INF #{op} #{n}" do
expect((-inf).send(op, n)).to eq lm
end
it "returns #{rm} for #{n} #{op} -INF" do
expect(n.send(op, -inf)).to eq rm
end
end
it "#{n} + NaN" do
expect(n + -nan).to be_nan
expect(-nan + n).to be_nan
end
end
describe RDF::Literal::Double::PI do
specify {expect(RDF::Literal::Double::PI.to_f).to eq Math::PI}
end
# Multiplication
{
-1 => described_class.new("-INF"),
0 => :nan,
1 => described_class.new("INF"),
}.each do |n, p|
it "returns #{p} for #{n} * INF" do
if p == :nan
expect(described_class.new(n) * inf).to be_nan
else
expect(described_class.new(n) * inf).to eq p
end
end
it "returns #{p} for INF * #{n}" do
if p == :nan
expect(inf * described_class.new(n)).to be_nan
else
expect(inf * described_class.new(n)).to eq p
end
end
end
# Comparison
{
"inf < inf" => [:<, "INF", "INF", false],
"inf > inf" => [:<, "INF", "INF", false],
"-inf < -inf" => [:<, "-INF", "-INF", false],
"0 < inf" => [:<, "0", "INF", true],
"0 < -inf" => [:<, "0", "-INF", false],
"0 > inf" => [:>, "0", "INF", false],
"0 > -inf" => [:>, "0", "-INF", true],
}.each do |n, (op, l, r, result)|
it "returns #{result} for #{l} #{op} #{r}" do
expect(described_class.new(l).send(op, described_class.new(r))).to eql result
end
end
it "adds infinities" do
expect(inf + inf).to eq inf
expect(inf + -inf).to be_nan
expect(-inf + -inf).to eq(-inf)
expect(-inf + inf).to be_nan
end
it "adds NaN" do
expect(inf + nan).to be_nan
expect(nan + nan).to be_nan
end
describe "#**" do
{
"-0e0^-3": [-0e0, -3, described_class.new('-INF')],
"-0e0^-3.0e0": [-0e0, -3.0e0, described_class.new('-INF')],
"-0e0^-3.1e0": [-0e0, -3.1e0, described_class.new('INF')],
"-0e0^3": [-0e0, 3, -0.0e0],
"-0e0^3.1e0": [-0e0, 3.1e0, 0.0e0],
"-1^-INF": [-1, described_class.new("-INF"), 1.0e0],
"-1^INF": [-1, described_class.new("INF"), 1.0e0],
"-2.5e0^2.0e0": [-2.5e0, 2.0e0, 6.25e0],
"-2^3": [-2, 3, -8],
"0e0^-3": [0e0, -3, described_class.new('INF')],
"0e0^-3.1e0": [0e0, -3.1e0, described_class.new('INF')],
"0e0^-4": [0e0, -4, described_class.new('INF')],
"0e0^3": [0e0, 3, 0.0e0],
"0e0^3.1e0": [0e0, 3.1e0, 0.0e0],
"0e0^4": [0e0, 4, 0.0e0],
"0^0": [0, 0, 1],
"0^4": [0, 4, 0],
"16^0.25e0": [16, 0.25e0, 2.0e0],
"16^0.5e0": [16, 0.5e0, 4.0e0],
"1^-INF": [1, described_class.new('-INF'), 1.0e0],
"1^INF": [1, described_class.new('INF'), 1.0e0],
"1^NaN": [1, described_class.new('NaN'), 1.0e0],
"2^-3": [2, -3, 0.125e0],
"2^0": [2, 0, 1],
"2^3": [2, 3, 8],
"INF^0": [described_class.new('INF'), 0, 1.0e0],
"NaN^0": [described_class.new('NaN'), 0, 1.0e0],
"PI^0": [described_class.const_get('PI'), 0, 1.0e0],
}.each do |name, (n, e, result)|
it name do
expect(RDF::Literal(n) ** RDF::Literal(e)).to eq RDF::Literal(result)
expect((RDF::Literal(n) ** RDF::Literal(e)).datatype).to eq RDF::Literal(result).datatype
end
end
context '#infinite?' do
{
"0e0^-3": [0e0, -3],
"0e0^-4": [0e0, -4],
"-0e0^-3": [-0e0, -3],
"0e0^-3.0e0": [0e0, -3.0e0],
"-0e0^-3.0e0": [-0e0, -3.0e0],
"0^-4": [0, -4],
}.each do |name, (n, e)|
specify(name) {expect((RDF::Literal(n) ** RDF::Literal(e)).infinite?).to be_truthy}
end
end
context '#nan?' do
{
#"-2.5e0^2.00000001e0": [-2.5e0, 2.00000001e0]
}.each do |name, (n, e)|
specify(name) {expect((RDF::Literal(n) ** RDF::Literal(e)).nan?).to be_truthy}
end
end
end
describe "#%" do
{
"0e0 % 3": [1.23e2, 0.6e1, 3.0e0],
}.each do |name, (n, e, result)|
it name do
expect(RDF::Literal(n) % RDF::Literal(e)).to eq RDF::Literal(result)
end
end
end
end
describe RDF::Literal::DateTime do
it_behaves_like 'RDF::Literal with datatype and grammar', "2010-01-01T00:00:00Z", RDF::XSD.dateTime
it_behaves_like 'RDF::Literal equality', "2010-01-01T00:00:00Z", DateTime.parse("2010-01-01T00:00:00Z")
it_behaves_like 'RDF::Literal lexical values', "2010-01-01T00:00:00Z"
it_behaves_like 'RDF::Literal canonicalization', RDF::XSD.dateTime, [
["2010-01-01T00:00:00Z", "2010-01-01T00:00:00Z", "12:00:00 AM UTC on Friday, 01 January 2010"],
["2010-01-01T00:00:00.0000Z", "2010-01-01T00:00:00Z", "12:00:00 AM UTC on Friday, 01 January 2010"],
["2010-01-01T00:00:00", "2010-01-01T00:00:00", "12:00:00 AM on Friday, 01 January 2010"],
["2010-01-01T00:00:00+00:00", "2010-01-01T00:00:00Z", "12:00:00 AM UTC on Friday, 01 January 2010"],
["2010-01-01T01:00:00+01:00", "2010-01-01T00:00:00Z", "01:00:00 AM +01:00 on Friday, 01 January 2010"],
["2009-12-31T23:00:00-01:00", "2010-01-01T00:00:00Z", "11:00:00 PM -01:00 on Thursday, 31 December 2009"],
["-2010-01-01T00:00:00Z", "-2010-01-01T00:00:00Z","12:00:00 AM UTC on Friday, 01 January -2010"],
["2014-09-01T12:13:14.567", "2014-09-01T12:13:14.567", "12:13:14 PM on Monday, 01 September 2014"],
["2014-09-01T12:13:14.567Z", "2014-09-01T12:13:14.567Z", "12:13:14 PM UTC on Monday, 01 September 2014"],
["2014-09-01T12:13:14.567-08:00","2014-09-01T20:13:14.567Z","12:13:14 PM -08:00 on Monday, 01 September 2014"],
]
it_behaves_like 'RDF::Literal validation', RDF::XSD.dateTime,
%w(
2010-01-01T00:00:00Z
2010-01-01T00:00:00.0000Z
2010-01-01T00:00:00
2010-01-01T00:00:00+00:00
2010-01-01T01:00:00+01:00
2009-12-31T23:00:00-01:00
-2010-01-01T00:00:00Z
20010-01-01T00:00:00Z
-20010-01-01T00:00:00Z
0052-01-01T00:00:00Z
-0052-01-01T00:00:00Z
),
%w(
foo
+2010-01-01T00:00:00Z
2010-01-01T00:00:00FOO
02010-01-01T00:00:00
2010-01-01
2010-1-1T00:00:00
0000-01-01T00:00:00
2010-07
2010
52-01-01T00:00:00Z
052-01-01T00:00:00Z
-52-01-01T00:00:00Z
-052-01-01T00:00:00Z
) + ['2010-01-01T00:00:00Z foo', 'foo 2010-01-01T00:00:00Z']
context "object values" do
{
DateTime.parse("2010-01-01T00:00:00Z") => ["2010-01-01T00:00:00Z", "2010-01-01T00:00:00Z"],
DateTime.parse("2010-02-01T00:00:00") => ["2010-02-01T00:00:00Z", "2010-02-01T00:00:00Z"],
DateTime.parse("2010-03-01T04:00:00+01:00") => ["2010-03-01T04:00:00+01:00", "2010-03-01T03:00:00Z"],
DateTime.parse("2009-12-31T04:00:00-01:00") => ["2009-12-31T04:00:00-01:00", "2009-12-31T05:00:00Z"],
DateTime.parse("-2010-01-01T00:00:00Z") => ["-2010-01-01T00:00:00Z", "-2010-01-01T00:00:00Z"],
}.each do |obj, (str, canon)|
it "to_str #{obj} to #{str.inspect}" do
expect(described_class.new(obj).to_s).to eql str
end
it "canonicalizes #{obj} to #{canon.inspect}" do
expect(described_class.new(obj, canonicalize: true).to_s).to eql canon
end
end
end
describe "#tz" do
{
"2010-06-21T11:28:01Z" => "Z",
"2010-12-21T15:38:02-08:00" => "-08:00",
"2008-06-20T23:59:00Z" => "Z",
"2011-02-01T01:02:03" => "",
}.each do |l, r|
it "#{l} => #{r}" do
expect(described_class.new(l).tz).to eq RDF::Literal(r)
end
end
end
describe "#timezone" do
{
"2010-06-21T11:28:01Z" => RDF::Literal("PT0H", datatype: RDF::XSD.dayTimeDuration),
"2010-12-21T15:38:02-08:00" => RDF::Literal("-PT8H", datatype: RDF::XSD.dayTimeDuration),
"2010-12-21T15:38:02+08:00" => RDF::Literal("PT8H", datatype: RDF::XSD.dayTimeDuration),
"2008-06-20T23:59:00Z" => RDF::Literal("PT0H", datatype: RDF::XSD.dayTimeDuration),
"2011-02-01T01:02:03" => nil,
}.each do |l, r|
it "#{l} => #{r.inspect}" do
expect(described_class.new(l).timezone).to eq r
end
end
end
describe "#adjust_to_timezone" do
{
# Spec examples
["2002-03-07T10:00:00"] => "2002-03-07T10:00:00Z",
["2002-03-07T10:00:00-07:00"] => "2002-03-07T17:00:00Z",
["2002-03-07T10:00:00", "-PT10H"] => "2002-03-07T10:00:00-10:00",
["2002-03-07T10:00:00-07:00", "-PT10H"] => "2002-03-07T07:00:00-10:00",
["2002-03-07T10:00:00-07:00", "PT10H"] => "2002-03-08T03:00:00+10:00",
["2002-03-07T00:00:00+01:00", "-PT8H"] => "2002-03-06T15:00:00-08:00",
["2002-03-07T10:00:00", nil] => "2002-03-07T10:00:00",
["2002-03-07T10:00:00-07:00", nil] => "2002-03-07T10:00:00",
}.each do |args, r|
if r == ArgumentError
it "#{args.inspect} raises ArgumentError" do
source = described_class.new(args.shift)
expect {source.adjust_to_timezone(*args)}.to raise_error(ArgumentError)
end
else
it "#{args.inspect} => #{r.inspect}" do
source = described_class.new(args.shift)
result = described_class.new(r)
expect(source.adjust_to_timezone(*args)).to eq result
end
end
end
{
# Test Suite https://github.com/w3c/qt3tests/blob/master/fn/adjust-dateTime-to-timezone.xml
"fn-adjust-dateTime-to-timezone1args-1": ["1970-01-01T00:00:00Z", "-PT10H", "1969-12-31T14:00:00-10:00"],
"fn-adjust-dateTime-to-timezone1args-2": ["1996-04-07T01:40:52Z", "-PT10H", "1996-04-06T15:40:52-10:00"],
"fn-adjust-dateTime-to-timezone1args-3": ["2030-12-31T23:59:59Z", "-PT10H", "2030-12-31T13:59:59-10:00"],
"fn-adjust-dateTime-to-timezone-1": ["2002-03-07T10:00:00-05:00", "-PT5H0M", "2002-03-07T10:00:00-05:00"],
"fn-adjust-dateTime-to-timezone-2": ["2002-03-07T10:00:00-07:00", "-PT5H0M", "2002-03-07T12:00:00-05:00"],
"fn-adjust-dateTime-to-timezone-3": ["2002-03-07T10:00:00", "-PT10H", "2002-03-07T10:00:00-10:00"],
"fn-adjust-dateTime-to-timezone-4": ["2002-03-07T10:00:00-07:00", "-PT10H", "2002-03-07T07:00:00-10:00"],
"fn-adjust-dateTime-to-timezone-5": ["2002-03-07T10:00:00-07:00", "PT10H", "2002-03-08T03:00:00+10:00"],
"fn-adjust-dateTime-to-timezone-6": ["2002-03-07T00:00:00+01:00", "-PT8H", "2002-03-06T15:00:00-08:00"],
"fn-adjust-dateTime-to-timezone-7": ["2002-03-07T10:00:00", "2002-03-07T10:00:00"],
"fn-adjust-dateTime-to-timezone-8": ["2002-03-07T10:00:00-07:00", nil, "2002-03-07T10:00:00"],
"fn-adjust-dateTime-to-timezone-11": ["2002-03-07T10:00:00-04:00", nil, "2002-03-07T10:00:00"],
"K-AdjDateTimeToTimezoneFunc-7": ["2001-02-03T08:02:00", "PT14H1M", ArgumentError],
"K-AdjDateTimeToTimezoneFunc-8": ["2001-02-03T08:02:00", "-PT14H1M", ArgumentError],
"K-AdjDateTimeToTimezoneFunc-9": ["2001-02-03T08:02:00", "PT14H0M0.001S", ArgumentError],
}.each do |title, (*args, r)|
it title do
source = described_class.new(args.shift)
if r == ArgumentError
expect {source.adjust_to_timezone(*args)}.to raise_error(ArgumentError)
else
result = described_class.new(r)
expect(source.adjust_to_timezone(*args)).to eq result
end
end
end
end
describe "#==" do
{
["2002-04-02T12:00:00-01:00", "2002-04-02T17:00:00+04:00"] => true,