Changeset - d1946cb32121
[Not reviewed]
default
0 2 0
drewp@bigasterisk.com - 8 months ago 2024-05-28 22:34:21
drewp@bigasterisk.com
bool support
2 files changed with 6 insertions and 0 deletions:
0 comments (0 inline, 0 general)
src/light9/typedgraph.py
Show inline comments
 
@@ -41,12 +41,13 @@ def _convLiteral(objType: Type[_ObjType]
 
    if _typeIncludes(objType, Literal):
 
        return cast(objType, x)
 

	
 
    for outType, dtypes in [
 
        (float, (XSD['integer'], XSD['double'], XSD['decimal'])),
 
        (int, (XSD['integer'],)),
 
        (bool, (XSD['boolean'],)), # todo tests
 
        (str, ()),
 
    ]:
 
        for t in _expandUnion(objType):
 
            if _typeIncludes(t, outType) and (not dtypes or x.datatype in dtypes):
 
                # e.g. user wants float and we have xsd:double
 
                return cast(objType, outType(x.toPython()))
src/light9/typedgraph_test.py
Show inline comments
 
@@ -16,12 +16,14 @@ g = cast(
 
        :bnode [];
 
        # see https://w3c.github.io/N3/spec/#literals for syntaxes.
 
        :int 0;
 
        :float1 0.0;
 
        :float2 1.0e1;
 
        :float3 0.5;
 
        :boolt true;
 
        :boolf false;
 
        :color "#ffffff"^^:hexColor;
 
        :definitelyAString "hello" .
 
'''))
 

	
 
subj = L9['subj']
 

	
 
@@ -86,12 +88,15 @@ class TestTypedValueReturnsBasicTypes:
 
        assert tv.datatype == XSD['double']
 

	
 
        tv = typedValue(Literal, g, subj, L9['color'])
 
        assert type(tv) == Literal
 
        assert tv.datatype == L9['hexColor']
 

	
 
    def test_getsBoolean(self):
 
        assert typedValue(bool, g, subj, L9['boolt'])
 
        assert not typedValue(bool, g, subj, L9['boolf'])
 

	
 
class TestTypedValueDoesntDoInappropriateUriStringConversions:
 

	
 
    def test_noUriToString(self):
 
        with pytest.raises(ConversionError):
 
            typedValue(str, g, subj, L9['uri'])
0 comments (0 inline, 0 general)