|
| 1 | +// Copyright 2025 Siemens AG |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package ld |
| 16 | + |
| 17 | +import ( |
| 18 | + "testing" |
| 19 | + |
| 20 | + "github.com/stretchr/testify/assert" |
| 21 | + "github.com/stretchr/testify/require" |
| 22 | +) |
| 23 | + |
| 24 | +func TestRdfToObject_NativeTypes(t *testing.T) { |
| 25 | + testCases := []struct { |
| 26 | + name string |
| 27 | + literal Literal |
| 28 | + expected map[string]any |
| 29 | + }{ |
| 30 | + { |
| 31 | + name: "Boolean true", |
| 32 | + literal: Literal{Value: "true", Datatype: XSDBoolean}, |
| 33 | + expected: map[string]any{"@value": true}, |
| 34 | + }, |
| 35 | + { |
| 36 | + name: "Boolean false", |
| 37 | + literal: Literal{Value: "false", Datatype: XSDBoolean}, |
| 38 | + expected: map[string]any{"@value": false}, |
| 39 | + }, |
| 40 | + { |
| 41 | + name: "Boolean True", |
| 42 | + literal: Literal{Value: "True", Datatype: XSDBoolean}, |
| 43 | + expected: map[string]any{"@value": "True", "@type": XSDBoolean}, |
| 44 | + }, |
| 45 | + { |
| 46 | + name: "Float", |
| 47 | + literal: Literal{Value: "3.141", Datatype: XSDFloat}, |
| 48 | + expected: map[string]any{"@value": float64(3.141)}, |
| 49 | + }, |
| 50 | + { |
| 51 | + name: "Double", |
| 52 | + literal: Literal{Value: "2.71828", Datatype: XSDDouble}, |
| 53 | + expected: map[string]any{"@value": float64(2.71828)}, |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "Integer", |
| 57 | + literal: Literal{Value: "42", Datatype: XSDInteger}, |
| 58 | + expected: map[string]any{"@value": int64(42)}, |
| 59 | + }, |
| 60 | + { |
| 61 | + name: "String without @type", |
| 62 | + literal: Literal{Value: "hello world", Datatype: XSDString}, |
| 63 | + expected: map[string]any{"@value": "hello world"}, |
| 64 | + }, |
| 65 | + { |
| 66 | + name: "Decimal", |
| 67 | + literal: Literal{Value: "3.141", Datatype: XSDDecimal}, |
| 68 | + expected: map[string]any{"@value": "3.141", "@type": XSDDecimal}, |
| 69 | + }, |
| 70 | + } |
| 71 | + for _, tc := range testCases { |
| 72 | + t.Run(tc.name, func(t *testing.T) { |
| 73 | + converted, err := RdfToObject(tc.literal, true) |
| 74 | + require.NoError(t, err) |
| 75 | + assert.Equal(t, tc.expected, converted) |
| 76 | + }) |
| 77 | + } |
| 78 | +} |
0 commit comments