Exemplo n.º 1
0
        public void WhenOneFileDoesntExist_ReportsErrorAndKeepsConverting()
        {
            var parser         = Substitute.For <ISketchParser>();
            var guid           = Guid.NewGuid();
            var sketchDocument = new SketchDocument(guid, new List <SketchPage>());

            parser.Parse(Arg.Any <SketchArchive>()).Returns(_ => sketchDocument);

            var builder   = Substitute.For <IUxBuilder>();
            var log       = new MessageListLogger();
            var converter = new Converter(parser, builder, log);

            var doesntExist = Path.Combine(TempDirectory, "IDontExist.sketch");
            var arrowSketch = Path.Combine(FilesDirectory, "Arrow.sketch");

            converter.Convert(new[] { doesntExist, arrowSketch }, TempDirectory);

            //Got error for the first file
            Assert.That(log.ErrorsAndWarnings().Count, Is.EqualTo(1));
            Assert.That(log.ErrorsAndWarnings()[0], Does.Match("Can't convert non-existing Sketch-file.*IDontExist.sketch"));

            //Called builder for the second file, and only that
            builder.ReceivedWithAnyArgs(1).Build(null, null);
            builder.Received(1).Build(sketchDocument, TempDirectory);
        }
        public void SketchFileWithoutCompatibilityVersionDoesNotFail()
        {
            // The first sketch-files in the open format doesn't seem to have compatibilityVersion field
            var archive    = Substitute.For <ISketchArchive>();
            var appVersion = 3.14;
            var version    = 91;

            var json =
                @"{
				'appVersion': '"                 + appVersion + @"',
				'build': 45422,
				'variant': 'NONAPPSTORE',
				'version': '"                 + version + @"'
			}"            ;

            using (var jsonStream = new MemoryStream(Encoding.ASCII.GetBytes(json))) {
                archive.OpenFile("meta.json")
                .Returns(jsonStream);

                var log = new MessageListLogger();

                CompatibilityChecker.Check("dummy.sketch", archive, log);

                Assert.AreEqual(1, log.ErrorsAndWarnings().Count);
                Assert.AreEqual(3, log.Messages.Count);
                Assert.That(log.ErrorsAndWarnings().First(),
                            Does.StartWith("WARNING:" + "\t" + "The sketch file was created with an older version"));
            }
        }
Exemplo n.º 3
0
        public void WhenOneFileFailsParsing_ReportsErrorAndKeepsConverting()
        {
            var parser         = Substitute.For <ISketchParser>();
            var guid           = Guid.NewGuid();
            var sketchDocument = new SketchDocument(guid, new List <SketchPage>());

            parser.Parse(Arg.Any <SketchArchive>()).Returns(
                _ => { throw new SketchParserException("Whenever I adjust my sail, I fail."); },
                _ => sketchDocument);

            var builder   = Substitute.For <IUxBuilder>();
            var log       = new MessageListLogger();
            var converter = new Converter(parser, builder, log);

            //Just some arbitrary valid sketch files to make sure `Load` works
            var arrowSketch = Path.Combine(FilesDirectory, "Arrow.sketch");
            var lineSketch  = Path.Combine(FilesDirectory, "Line.sketch");

            converter.Convert(new [] { arrowSketch, lineSketch }, TempDirectory);

            //Got error for the first file
            Assert.That(log.ErrorsAndWarnings().Count, Is.EqualTo(1));
            Assert.That(log.ErrorsAndWarnings()[0], Does.Match("Arrow.sketch.*I fail"));

            //Called builder for the second file, and only that
            builder.ReceivedWithAnyArgs(1).Build(null, null);
            builder.Received(1).Build(sketchDocument, TempDirectory);
        }
Exemplo n.º 4
0
        public void UnsupportedLayerWarnsAndSkips()
        {
            var mockSketchArchive = MockSketchArchive.Create(Snippets.JellyfishLayerJson);
            var log = new MessageListLogger();

            var document = new SketchParser(log).Parse(mockSketchArchive);

            Assert.That(document.Pages.Count, Is.EqualTo(1));
            Assert.That(document.Pages[0].Layers.Count, Is.EqualTo(0));
            Assert.That(log.ErrorsAndWarnings().Count, Is.EqualTo(1));
            Assert.That(log.ErrorsAndWarnings()[0], Does.Match("Skipping layer 'Jellyfish' of unsupported type 'jellyfish'"));
        }
Exemplo n.º 5
0
        public void ParseErrorInLayerLogsAndSkips()
        {
            var mockSketchArchive = MockSketchArchive.Create(Snippets.PathInvalidJson);
            var log = new MessageListLogger();

            var document = new SketchParser(log).Parse(mockSketchArchive);

            Assert.That(document.Pages.Count, Is.EqualTo(1));
            Assert.That(document.Pages[0].Layers.Count, Is.EqualTo(0));
            Assert.That(log.ErrorsAndWarnings().Count, Is.EqualTo(1));
            Assert.That(log.ErrorsAndWarnings()[0], Does.Match("Failed to parse layer"));
        }
Exemplo n.º 6
0
        public void CanBeCalledTwice()
        {
            var log         = new MessageListLogger();
            var converter   = new Converter(new SketchParser(log), new SymbolsUxBuilder(log), log);
            var arrowSketch = Path.Combine(FilesDirectory, "Arrow.sketch");

            Assert.That(File.Exists(arrowSketch));
            converter.Convert(new [] { arrowSketch }, TempDirectory);
            Assert.That(log.ErrorsAndWarnings(), Is.Empty);
            converter.Convert(new [] { arrowSketch }, TempDirectory);
            Assert.That(log.ErrorsAndWarnings(), Is.Empty);
        }
Exemplo n.º 7
0
        public void UnimplementedLayerTypeFailsGracefullyWithWarning()
        {
            var log     = new MessageListLogger();
            var builder = new UxBuilder(new SymbolClassNameBuilder(), Substitute.For <IAssetEmitter>(), log);
            var uxNode  = builder.BuildLayer(new UnimplementedLayerType());

            Assert.That(log.ErrorsAndWarnings().Count, Is.EqualTo(1));
            var expected = "Unimplemented layer type: UnimplementedLayerType";

            Assert.That(log.ErrorsAndWarnings()[0], Does.Match(expected));
            var ux = uxNode.SerializeUx(new UxSerializerContext());

            Assert.That(ux, Does.Match("<!-- " + expected + " -->"));
        }
Exemplo n.º 8
0
        public void NestedSketchSymbolCreatesUxInstance()
        {
            var childSymbol = DocumentBuilder.SketchSymbolMaster("Inner").WithLayer(DocumentBuilder.Rectangle("R1"));

            var document = DocumentBuilder.SketchDocument()
                           .WithSymbol(childSymbol)
                           .WithSymbol(DocumentBuilder.SketchSymbolMaster("Outer").WithLayer(DocumentBuilder.SymbolInstanceOf(childSymbol)));

            var logger  = new MessageListLogger();
            var builder = new SymbolsUxBuilder(logger);

            builder.Build(document, _outputDirectory);

            var files = Directory.EnumerateFiles(_outputDirectory);

            Assert.AreEqual(2, files.Count());
            Assert.IsTrue(files.First().EndsWith("Inner.ux"));
            Assert.IsTrue(files.Last().EndsWith("Outer.ux"));
            Assert.AreEqual(0, logger.ErrorsAndWarnings().Count);

            var ux = File.ReadAllText(files.Last());

            Assert.IsTrue(ux.Contains("<Sketch.Inner"));
            Assert.IsTrue(ux.Contains("</Sketch.Inner>"));
        }
Exemplo n.º 9
0
        public void SymbolWithReservedNameGetsSkippedAndLogged()
        {
            var sketchSymbolMaster = DocumentBuilder.SketchSymbolMaster("class");
            var document           = DocumentBuilder.SketchDocument()
                                     .WithSymbol(sketchSymbolMaster);

            var logger  = new MessageListLogger();
            var builder = new SymbolsUxBuilder(logger);

            builder.Build(document, _outputDirectory);

            var files = Directory.EnumerateFiles(_outputDirectory);

            Assert.That(files.Count(), Is.EqualTo(0));
            Assert.That(logger.ErrorsAndWarnings().Count, Is.EqualTo(1));
            Assert.That(logger.ErrorsAndWarnings()[0], Does.Contain("The symbol name 'class' is a reserved word."));
        }
Exemplo n.º 10
0
        public void SymbolWithInvalidCharacterInNameGetsSkippedAndLogged()
        {
            var sketchSymbolMaster = DocumentBuilder.SketchSymbolMaster("a b");
            var document           = DocumentBuilder.SketchDocument()
                                     .WithSymbol(sketchSymbolMaster);

            var logger  = new MessageListLogger();
            var builder = new SymbolsUxBuilder(logger);

            builder.Build(document, _outputDirectory);

            var files = Directory.EnumerateFiles(_outputDirectory);

            Assert.That(files.Count(), Is.EqualTo(0));
            Assert.That(logger.ErrorsAndWarnings().Count, Is.EqualTo(1));
            Assert.That(logger.ErrorsAndWarnings()[0], Does.Contain("The symbol name 'a b' contains an invalid character"));
        }
Exemplo n.º 11
0
        public void SymbolWithNoLayersDoesNotGetWritten()
        {
            var document = DocumentBuilder.SketchDocument()
                           .WithSymbol(DocumentBuilder.SketchSymbolMasterWithNoLayers("Harry"));

            var logger  = new MessageListLogger();
            var builder = new SymbolsUxBuilder(logger);

            builder.Build(document, _outputDirectory);

            var file = Directory.EnumerateFiles(_outputDirectory);

            Assert.AreEqual(0, file.Count());

            Assert.AreEqual(1, logger.ErrorsAndWarnings().Count);
            Assert.That(logger.ErrorsAndWarnings().First(),
                        Does.Match("Skipping symbol 'Sketch.Harry' which has no supported layers."));
        }
Exemplo n.º 12
0
        public void TwoSymbolsWithSameNameGivesErrorAndOnlyWritesOne()
        {
            var document = DocumentBuilder.SketchDocument()
                           .WithSymbol(DocumentBuilder.SketchSymbolMaster("NameOfTheGame").WithLayer(DocumentBuilder.Rectangle("R")))
                           .WithSymbol(DocumentBuilder.SketchSymbolMaster("NameOfTheGame").WithLayer(DocumentBuilder.Rectangle("R")));

            var logger  = new MessageListLogger();
            var builder = new SymbolsUxBuilder(logger);

            builder.Build(document, _outputDirectory);


            var files = Directory.EnumerateFiles(_outputDirectory).ToList();

            Assert.That(files.Count(), Is.EqualTo(1));
            Assert.That(files.First(), Does.EndWith("NameOfTheGame.ux"));
            Assert.That(logger.ErrorsAndWarnings().Count, Is.EqualTo(1));
            Assert.That(logger.ErrorsAndWarnings()[0], Does.Match("More than one symbol named 'NameOfTheGame' was found!"));
        }
Exemplo n.º 13
0
        //This is important since we for instance parse pages in parallel
        public void ReportsAggregateExceptions()
        {
            var manyExceptions = new AggregateException(new List <Exception> {
                new Exception("foo"), new Exception("bar")
            });
            var parser = Substitute.For <ISketchParser>();

            parser.Parse(Arg.Any <SketchArchive>()).Throws(manyExceptions);

            var builder   = Substitute.For <IUxBuilder>();
            var log       = new MessageListLogger();
            var converter = new Converter(parser, builder, log);

            //Arbitrary existing file, just so we can get to `parser.Parse()`
            var arrowSketch = Path.Combine(FilesDirectory, "Arrow.sketch");

            converter.Convert(new [] { arrowSketch }, TempDirectory);
            Assert.That(log.ErrorsAndWarnings().Count, Is.EqualTo(2));
            Assert.That(log.ErrorsAndWarnings(), Has.Some.Match("foo"));
            Assert.That(log.ErrorsAndWarnings(), Has.Some.Match("bar"));
        }
Exemplo n.º 14
0
        public void SketchFileProducedWithOlderVersionThanSupportedGivesWarning()
        {
            var archive             = Substitute.For <ISketchArchive>();
            var appVerion           = 3.14;
            var version             = CompatibilityChecker.SketchCompatibilityVersion - 3;
            var compatibiltyVersion = version;

            var json = GenerateMetaJson(appVerion, compatibiltyVersion, version);

            using (var jsonStream = new MemoryStream(Encoding.ASCII.GetBytes(json))) {
                archive.OpenFile("meta.json")
                .Returns(jsonStream);

                var log = new MessageListLogger();

                CompatibilityChecker.Check("dummy.sketch", archive, log);

                Assert.AreEqual(1, log.ErrorsAndWarnings().Count);
                Assert.AreEqual(3, log.Messages.Count);
                Assert.That(log.ErrorsAndWarnings().First(),
                            Does.StartWith("WARNING:" + "\t" + "The sketch file was created with an older version"));
            }
        }
Exemplo n.º 15
0
        public void ParseLayerWithVerticalFlip()
        {
            var mockSketchArchive = MockSketchArchive.Create(Snippets.ShapeWithVerticalFlip);
            var log = new MessageListLogger();

            var sketchDocument = new SketchParser(log).Parse(mockSketchArchive);

            var shapePath = sketchDocument.Pages[0].Layers[0] as SketchShapePath;

            Assert.NotNull(shapePath);
            Assert.That(shapePath.Path.IsClosed, Is.False);
            Assert.That(shapePath.IsFlippedVertical, Is.True);
            Assert.That(shapePath.IsFlippedHorizontal, Is.False);
            Assert.That(log.ErrorsAndWarnings().Count, Is.Zero);
        }
Exemplo n.º 16
0
        public void ConvertingNonZipArchiveSketchFileReportsError()
        {
            var invalidSketchArchive = Path.Combine(Path.GetTempPath(), "invalid.sketch");

            using (var sw = new StreamWriter(invalidSketchArchive))
            {
                sw.WriteLine("This is not a sketch archive");
            }

            var log       = new MessageListLogger();
            var converter = new Converter(new SketchParser(log), new SymbolsUxBuilder(log), log);

            converter.Convert(new [] { invalidSketchArchive }, Path.GetTempPath());
            Assert.That(log.ErrorsAndWarnings().First(), Does.Match(invalidSketchArchive.Replace(@"\", @"\\") + ".*Can't open sketch file. This file might be on an old format."));

            File.Delete(invalidSketchArchive);
        }
Exemplo n.º 17
0
        public void TwoSketchSymbolsProducesTwoUxClassesInTwoFiles()
        {
            var document = DocumentBuilder.SketchDocument()
                           .WithSymbol(DocumentBuilder.SketchSymbolMaster("MySymbol").WithLayer(DocumentBuilder.Rectangle("R")))
                           .WithSymbol(DocumentBuilder.SketchSymbolMaster("MySymbol2").WithLayer(DocumentBuilder.Rectangle("R2")));

            var logger  = new MessageListLogger();
            var builder = new SymbolsUxBuilder(logger);

            builder.Build(document, _outputDirectory);


            var files = Directory.EnumerateFiles(_outputDirectory).ToList();

            Assert.AreEqual(2, files.Count());
            Assert.IsTrue(files.First().EndsWith("MySymbol.ux"));
            Assert.IsTrue(files.Last().EndsWith("MySymbol2.ux"));
            Assert.AreEqual(0, logger.ErrorsAndWarnings().Count);
        }
Exemplo n.º 18
0
        public void BuildUxForSketchShapeGroupWithoutMaskingGivesNoWarnings()
        {
            var rectangle = new SketchRectangle(
                CreateLayer(5, 5),
                new SketchPath(CurvePointUtils.RectanglePath(new CornerRadius(2)), true),
                SketchBooleanOperation.NoOperation);

            var parentGroup =
                new SketchGroup(
                    new SketchLayer(
                        new SketchLayer(
                            Guid.Empty,
                            new SketchLayer(
                                CreateLayer(20, 20),
                                new List <SketchLayer>()),
                            new SketchRect(0, 0, 10, 10, false),
                            "Dummy",
                            false,
                            DummyAlignment(),
                            0, false, false,
                            Optional.None(),
                            new List <SketchLayer>()),
                        new List <SketchLayer> {
                rectangle
            }));

            rectangle.Parent = parentGroup;

            var log       = new MessageListLogger();
            var uxBuilder = new UxBuilder(new SymbolClassNameBuilder(), Substitute.For <IAssetEmitter>(), log);

            var uxNode = uxBuilder.BuildLayer(parentGroup);

            Assert.That(uxNode, Is.Not.Null);

            var ux = uxNode.SerializeUx(new UxSerializerContext());

            Assert.That(ux, Is.EqualTo("<Panel Width=\"50%\" Height=\"50%\" Alignment=\"TopLeft\" Margin=\"0\">\n\t<!-- Dummy -->\n\t<Rectangle CornerRadius=\"2\" Width=\"50%\" Height=\"50%\" Alignment=\"TopLeft\" Margin=\"0\">\n\t\t<!-- Dummy -->\n\t</Rectangle>\n</Panel>"));
            Assert.That(log.ErrorsAndWarnings(), Is.Empty);
        }
Exemplo n.º 19
0
        public void BuildUxForSketchShapeGroupWithMaskingGivesUnsupportedWarnings()
        {
            var shapeGroup = new SketchShapeGroup(
                new SketchLayer(CreateLayer(5, 5), new List <SketchLayer>()),
                Mask);

            var parentGroup =
                new SketchGroup(
                    new SketchLayer(
                        new SketchLayer(
                            Guid.Empty,
                            new SketchLayer(
                                CreateLayer(20, 20),
                                new List <SketchLayer>()),
                            new SketchRect(0, 0, 10, 10, false),
                            "Dummy",
                            false,
                            DummyAlignment(),
                            0, false, false,
                            Optional.None(),
                            new List <SketchLayer>()),
                        new List <SketchLayer> {
                shapeGroup
            }));

            shapeGroup.Parent = parentGroup;

            var log       = new MessageListLogger();
            var uxBuilder = new UxBuilder(new SymbolClassNameBuilder(), Substitute.For <IAssetEmitter>(), log);

            var uxNode = uxBuilder.BuildLayer(parentGroup);

            Assert.That(uxNode, Is.Not.Null);

            var ux = uxNode.SerializeUx(new UxSerializerContext());

            Assert.That(ux, Is.EqualTo("<Panel Width=\"50%\" Height=\"50%\" Alignment=\"TopLeft\" Margin=\"0\">\n\t<!-- Dummy -->\n\t<!-- Masked shape group is not supported in UX -->\n</Panel>"));
            Assert.That(log.ErrorsAndWarnings().First(), Does.Match("WARNING:\tMasked shapes are not supported Dummy"));
        }