142 lines
7.1 KiB
Luau
142 lines
7.1 KiB
Luau
-- codecs/model/Vrml tests: the classic-encoding lexer and parser, the XML encoding's adapter, detection, errors.
|
|
local T = require("@tests/T")
|
|
local Vrml = require("@rosetta/codecs/model/Vrml")
|
|
local Fields = require("@rosetta/codecs/model/Vrml/Fields")
|
|
local Err = require("@rosetta/core/Err")
|
|
|
|
local t = T.suite("vrml codec")
|
|
|
|
do
|
|
local g = assert(Vrml.parse([[#VRML V2.0 utf8
|
|
# a comment, then commas as blanks
|
|
DEF A Transform { translation 1,2,3 # trailing comment
|
|
children [ Shape { geometry IndexedFaceSet { coordIndex [ 0, 1, 2, -1 ] coord Coordinate { point [ 0 0 0, 1e1 0 0, .5 -2.5E-1 +3 ] } } } ] }
|
|
WorldInfo { title "say \"hi\" \\ there" info [ "a" "b" ] }
|
|
DEF P PixelTexture { image 1 1 4 0xFF8000FF }
|
|
USE A
|
|
]]))
|
|
t:eq(#g.roots, 4, "statements")
|
|
t:eq(g.header.version, "2.0", "header")
|
|
local a = g.roots[1]
|
|
t:eq(a.def, "A", "DEF")
|
|
t:eq(table.concat(Fields.vec(a, "translation", { 0, 0, 0 }), " "), "1 2 3", "commas are blanks")
|
|
local coord = Fields.node(Fields.node(Fields.nodes(a, "children")[1], "geometry"), "coord")
|
|
t:eq(table.concat(Fields.nums(coord, "point") :: { number }, " "), "0 0 0 10 0 0 0.5 -0.25 3", "exponents, leading dot, plus sign")
|
|
t:eq(Fields.str(g.roots[2], "title"), 'say "hi" \\ there', "string escapes")
|
|
t:eq(#Fields.strs(g.roots[2], "info"), 2, "MFString")
|
|
t:eq((Fields.nums(g.roots[3], "image") :: { number })[4], 0xFF8000FF, "hex numbers")
|
|
t:check(g.roots[4] == a, "USE is the same node")
|
|
end
|
|
|
|
do
|
|
local g = assert(Vrml.parse([[#VRML V2.0 utf8
|
|
PROTO Outer [ field SFFloat size 1 exposedField SFColor tint 1 1 1 eventIn SFBool flip ] {
|
|
PROTO Inner [ field SFFloat r 1 ] { Sphere { radius IS r } }
|
|
DEF G Group { children [ Shape { geometry Inner { r IS size } appearance Appearance { material DEF M Material { diffuseColor IS tint } } } ] }
|
|
DEF Clock TimeSensor { }
|
|
DEF I PositionInterpolator { key [ 0 1 ] keyValue [ 0 0 0 1 1 1 ] }
|
|
ROUTE Clock.fraction_changed TO I.set_fraction
|
|
ROUTE I.value_changed TO G.set_translation
|
|
}
|
|
EXTERNPROTO Far [ field SFFloat x ] [ "far.wrl#Far" "urn:far" ]
|
|
DEF O1 Outer { size 3 tint 0 1 0 }
|
|
Outer { }
|
|
Far { x 2 }
|
|
DEF S Script { field SFNode target USE O1 eventIn SFTime touch url "javascript: function touch() {}" }
|
|
IMPORT inl.Thing AS Thing
|
|
EXPORT S AS Exported
|
|
]]))
|
|
t:eq(#g.roots, 4, "proto instances, extern instance, script")
|
|
local o1, o2 = g.roots[1], g.roots[2]
|
|
t:eq(o1.type, "Group", "an instance is its body's first node")
|
|
t:eq(o1.def, "O1", "the instance's DEF name")
|
|
local function radius(o: any): number
|
|
local sh = Fields.nodes(o, "children")[1]
|
|
return Fields.num(Fields.node(sh, "geometry"), "radius", -1)
|
|
end
|
|
t:eq(radius(o1), 3, "IS through a nested PROTO (instance value)")
|
|
t:eq(radius(o2), 1, "IS default")
|
|
local mat = Fields.node(Fields.node(Fields.nodes(o1, "children")[1], "appearance"), "material")
|
|
t:eq(table.concat(Fields.vec(mat, "diffuseColor", { 9, 9, 9 }), " "), "0 1 0", "IS on an SFColor")
|
|
local mat2 = Fields.node(Fields.node(Fields.nodes(o2, "children")[1], "appearance"), "material")
|
|
t:check(mat ~= mat2, "each instance has its own copy of the body")
|
|
t:eq(#g.routes, 4, "the body's ROUTEs, per instance")
|
|
t:check(g.routes[2].to == o1 or g.routes[2].to.fields == o1.fields, "routes point at the instance's nodes")
|
|
t:eq(g.roots[3].type, "Far", "EXTERNPROTO instance keeps its type")
|
|
t:check(g.roots[3].extern == true, "EXTERNPROTO instance flagged")
|
|
t:eq(Fields.node(g.roots[4], "target"), o1, "Script interface field with a USE value")
|
|
end
|
|
|
|
do
|
|
local g = assert(Vrml.parse("#X3D V3.3 utf8\nPROFILE Immersive\nCOMPONENT CADGeometry:2\nMETA \"author\" \"me\"\nUNIT length foot 0.3048\nGroup { }\n"))
|
|
t:check(g.header.x3d and g.header.version == "3.3" and g.header.profile == "Immersive", "X3D header, PROFILE")
|
|
t:eq(g.components[1], "CADGeometry:2", "COMPONENT")
|
|
t:eq(g.meta.author, "me", "META")
|
|
t:eq(g.units.length.factor, 0.3048, "UNIT")
|
|
local s = assert(Vrml.build(g))
|
|
t:eq(s.meta.unit, 0.3048, "UNIT length -> meta.unit")
|
|
end
|
|
|
|
do
|
|
local g = assert(Vrml.graph([[<?xml version="1.0"?>
|
|
<X3D profile="Interchange" version="4.0"><head><component name="Shaders" level="1"/></head><Scene>
|
|
<ProtoDeclare name="Ball"><ProtoInterface><field name="r" type="SFFloat" accessType="initializeOnly" value="2"/></ProtoInterface>
|
|
<ProtoBody><Shape><Sphere><IS><connect nodeField="radius" protoField="r"/></IS></Sphere></Shape></ProtoBody></ProtoDeclare>
|
|
<Group DEF="G"><ProtoInstance name="Ball"><fieldValue name="r" value="5"/></ProtoInstance>
|
|
<ProtoInstance name="Ball" containerField="children"/></Group>
|
|
<Shape><Appearance><ImageTexture url='"a.png" "b.png"'/></Appearance><Box containerField="geometry" size="1, 2, 3" solid="FALSE"/></Shape>
|
|
<Transform USE="G"/>
|
|
</Scene></X3D>]]))
|
|
t:check(g.header.xml and g.header.version == "4.0", "XML header")
|
|
t:eq(g.components[1], "Shaders:1", "head component")
|
|
local grp = g.roots[1]
|
|
local kids = Fields.nodes(grp, "children")
|
|
t:eq(#kids, 2, "ProtoInstances in children")
|
|
t:eq(Fields.num(Fields.node(kids[1], "geometry"), "radius", -1), 5, "fieldValue")
|
|
t:eq(Fields.num(Fields.node(kids[2], "geometry"), "radius", -1), 2, "ProtoInterface default")
|
|
local shape = g.roots[2]
|
|
local box = Fields.node(shape, "geometry")
|
|
t:eq(table.concat(Fields.vec(box, "size", { 9, 9, 9 }), " "), "1 2 3", "numbers with commas")
|
|
t:eq(Fields.bool(box, "solid", true), false, "XML booleans (any case)")
|
|
local tex = Fields.node(Fields.node(shape, "appearance"), "texture")
|
|
local urls = Fields.strs(tex, "url")
|
|
t:check(#urls == 2 and urls[2] == "b.png", "quoted MFString")
|
|
t:eq(g.roots[3], grp, "USE in XML")
|
|
local single = { type = "ImageTexture", fields = { url = "plain name.png" } }
|
|
t:eq(Fields.strs(single, "url")[1], "plain name.png", "unquoted single MFString")
|
|
end
|
|
|
|
do
|
|
local function code(text: string): string?
|
|
local _, e = Vrml.graph(text)
|
|
return e and e.code
|
|
end
|
|
t:eq(code("#VRML V2.0 utf8\nShape { geometry Box { }"), Err.CORRUPT, "unclosed node")
|
|
t:eq(code("#VRML V2.0 utf8\nShape { geometry USE X }"), Err.CORRUPT, "undefined USE")
|
|
t:eq(code("#VRML V2.0 utf8\nROUTE A.b TO"), Err.CORRUPT, "bad ROUTE")
|
|
t:eq(code("#VRML V2.0 utf8\nWorldInfo { title \"x }"), Err.TRUNCATED, "unterminated string")
|
|
t:eq(code("#VRML V1.0 ascii\n"), Err.UNSUPPORTED, "VRML 1.0")
|
|
t:eq(code("\224\0\0\1rest"), Err.UNSUPPORTED, "Fast Infoset X3D")
|
|
t:eq(code("#VRML V2.0 utf8\n" .. string.rep("Group { children ", 250)), Err.LIMIT, "nesting limit")
|
|
t:eq(code("<X3D><Scene><Group></Scene></X3D>"), Err.CORRUPT, "bad XML")
|
|
local _, e = Vrml.encode({} :: any)
|
|
t:eq(e and e.code, Err.ARG, "encode junk")
|
|
end
|
|
|
|
do
|
|
local samples = {
|
|
"#VRML V2.0 utf8\nPROTO P [ field SFFloat a 1 ] { Box { size IS a IS a } }\nDEF X P { a 2 } USE X ROUTE X.a TO X.b\n",
|
|
"#X3D V3.3 utf8\nPROFILE Full\nShape { geometry Extrusion { spine [ 0 0 0 0 1 0 1 1 1 ] crossSection [ 0 0 1 0 1 1 0 0 ] } }\n",
|
|
"<X3D><Scene><ProtoDeclare name='A'><ProtoBody><Box/></ProtoBody></ProtoDeclare><ProtoInstance name='A'/><Shape><IndexedFaceSet coordIndex='0 1 2 -1 2 3 0'><Coordinate point='0 0 0 1 0 0 1 1 0 0 1 0'/></IndexedFaceSet></Shape></Scene></X3D>",
|
|
}
|
|
t:fuzz("graph + build", function(b: buffer)
|
|
local g = Vrml.graph(b)
|
|
if g then
|
|
return Vrml.build(g)
|
|
end
|
|
return nil
|
|
end, samples, { mutations = 400 })
|
|
end
|
|
|
|
t:done()
|