Ejemplo n.º 1
0
        public void SetThrowsIfArgumentsAreNullOrEmpty()
        {
            Replacer map = new Replacer();

            Assert.That(() => map.Add(null, "a"), Throws.ArgumentNullException);
            Assert.That(() => map.Add("a", null), Throws.ArgumentNullException);
            Assert.That(() => map.Add(string.Empty, "a"), Throws.ArgumentNullException);
            Assert.That(() => map.Add("a", string.Empty), Throws.ArgumentNullException);
        }
Ejemplo n.º 2
0
        public void TransformChars()
        {
            Replacer replacer = new Replacer();

            replacer.Add("a", "z");
            replacer.Add("b", "y");

            Assert.That(replacer.TransformForward("ababtr"), Is.EqualTo("zyzytr"));
            Assert.That(replacer.TransformBackward("zyzytr"), Is.EqualTo("ababtr"));
        }
Ejemplo n.º 3
0
        public void TransformChars()
        {
            Replacer replacer = new Replacer();

            replacer.Add("a", "z");
            replacer.Add("b", "y");

            Assert.That(replacer.Transform("ababtr", true), Is.EqualTo("zyzytr"));
            Assert.That(replacer.Transform("zyzytr", false), Is.EqualTo("ababtr"));
        }
Ejemplo n.º 4
0
        public void TransformStrings()
        {
            Replacer replacer = new Replacer();

            replacer.Add("<br/>", "\n");
            replacer.Add("z", ".");

            Assert.That(replacer.TransformForward("zz<br/>zz"), Is.EqualTo("..\n.."));
            Assert.That(replacer.TransformBackward("..\n.."), Is.EqualTo("zz<br/>zz"));
        }
Ejemplo n.º 5
0
        public void TransformStrings()
        {
            Replacer replacer = new Replacer();

            replacer.Add("<br/>", "\n");
            replacer.Add("z", ".");

            Assert.That(replacer.Transform("zz<br/>zz", true), Is.EqualTo("..\n.."));
            Assert.That(replacer.Transform("..\n..", false), Is.EqualTo("zz<br/>zz"));
        }
Ejemplo n.º 6
0
        public void SetReplacesExistingEntry()
        {
            Replacer map = new Replacer();

            map.Add("a", "b");
            map.Add("a", "c");

            Assert.That(map.Map.Count, Is.EqualTo(1));
            Assert.That(map.Map[0].Original, Is.EqualTo("a"));
            Assert.That(map.Map[0].Modified, Is.EqualTo("c"));
        }
Ejemplo n.º 7
0
        public void TransformWithModifiedInOriginal()
        {
            Replacer replacer = new Replacer();

            replacer.Add("a", "b");
            replacer.Add("b", "c");

            Assert.That(replacer.Transform("aabaa", true), Is.EqualTo("bbcbb"));
            Assert.That(replacer.Transform("bbcbb", false), Is.EqualTo("aabaa"));

            replacer.Add("b", "a");

            Assert.That(replacer.Transform("aabaa", true), Is.EqualTo("bbabb"));
            Assert.That(replacer.Transform("bbabb", false), Is.EqualTo("aabaa"));
        }
Ejemplo n.º 8
0
        public void TransformWithMultipleSourceMatchesTakesOrder()
        {
            Replacer replacer = new Replacer();

            replacer.Add("a", "z");
            replacer.Add("ab", "y");

            Assert.That(replacer.Transform("aabaa", true), Is.EqualTo("zyzz"));
            Assert.That(replacer.Transform("zyzz", false), Is.EqualTo("aabaa"));

            replacer = new Replacer();
            replacer.Add("ab", "y");
            replacer.Add("a", "z");

            Assert.That(replacer.Transform("aabaa", true), Is.EqualTo("zzbzz"));
            Assert.That(replacer.Transform("zzbzz", false), Is.EqualTo("aabaa"));
        }
Ejemplo n.º 9
0
        public void TransformEmptyLineDoesNotThrowException()
        {
            Replacer replacer = new Replacer();

            replacer.Add("a", "b");

            Assert.That(replacer.Transform(string.Empty, true), Is.EqualTo(string.Empty));
        }
Ejemplo n.º 10
0
        public void TransformNullThrowException()
        {
            Replacer replacer = new Replacer();

            replacer.Add("a", "b");

            Assert.That(() => replacer.Transform(null, true), Throws.ArgumentNullException);
        }
Ejemplo n.º 11
0
        public void RemoveMapEntry()
        {
            Replacer replacer = new Replacer();

            replacer.Add("a", "b");
            Assert.That(replacer.Map.Count, Is.EqualTo(1));
            replacer.Remove("a");
            Assert.That(replacer.Map.Count, Is.EqualTo(0));
        }
Ejemplo n.º 12
0
        public void SetAddsCharToMap()
        {
            Replacer map = new Replacer();

            Assert.That(map.Map.Count, Is.EqualTo(0));

            map.Add("a", "b");
            Assert.That(map.Map.Count, Is.EqualTo(1));
            Assert.That(map.Map[0].Original, Is.EqualTo("a"));
            Assert.That(map.Map[0].Modified, Is.EqualTo("b"));
        }
Ejemplo n.º 13
0
        public void RemoveFakeMapEntry()
        {
            Replacer replacer = new Replacer();

            replacer.Add("a", "b");
            Assert.That(
                () => replacer.Remove("c"),
                Throws.InstanceOf <KeyNotFoundException>());
            Assert.That(
                () => replacer.Remove("b"),
                Throws.InstanceOf <KeyNotFoundException>());
        }
Ejemplo n.º 14
0
        public void RemoveEmptyMapEntry()
        {
            Replacer replacer = new Replacer();

            replacer.Add("a", "b");
            Assert.That(
                () => replacer.Remove(string.Empty),
                Throws.ArgumentNullException);
            Assert.That(
                () => replacer.Remove(null),
                Throws.ArgumentNullException);
        }
Ejemplo n.º 15
0
        public void AddDictionary(string file)
        {
            if (!File.Exists(file))
            {
                throw new FileNotFoundException();
            }

            var openFile = new Yarhl.IO.TextReader(
                DataStreamFactory.FromFile(file, FileOpenMode.Read));

            if (openFile.Stream.Length == 0)
            {
                throw new Exception("The file is empty.");
            }

            var separatorLine = openFile.ReadLine();

            if (!separatorLine.Contains("separator="))
            {
                throw new Exception("The separator has not been set.");
            }

            if (separatorLine.Length != 11)
            {
                throw new Exception("The separator not support multiple chars like == or ::");
            }

            var separator = separatorLine[10];

            do
            {
                var line = openFile.ReadLine();

                if (string.IsNullOrWhiteSpace(line))
                {
                    continue;
                }

                var textSplit = line.Split(separator);

                if (textSplit.Length != 2)
                {
                    throw new Exception("The original or modified string contains the separator or is missing, please check your separator.");
                }

                replacer.Add(textSplit[0], textSplit[1]);
            } while (!openFile.Stream.EndOfStream);
        }
Ejemplo n.º 16
0
        private void readTable()
        {
            string line;

            string[] chars;
            var      reader = new TextReader(DataStreamFactory.FromFile(tablePath, FileOpenMode.Read));

            while (!reader.Stream.EndOfStream)
            {
                line = reader.ReadLine();
                if (string.IsNullOrWhiteSpace(line))
                {
                    continue;
                }
                if (line[0] == '#')
                {
                    continue;
                }

                chars = line.Split('=');
                replacer.Add(chars[1], chars[0]);
            }
        }
        public override bool Execute()
        {
            _cachedItems.Clear();
            var replacer = new Replacer();

            replacer.Add(new EnvironmentReplacement());
            replacer.Add(CreateNugetReplacer());
            replacer.Add(CreateBuildReplacer());

            foreach (var client in NSwagClients)
            {
                var restoredClient = client.Restore();
                try
                {
                    var parsedClient = restoredClient.Parse();
                    Log.LogMessage(MessageImportance.Normal, $"Analyzing Swagger document for '{parsedClient.ClassName}' client");

                    var declaration           = parsedClient.Declaration.Parsed;
                    var documentCacheFilePath = Path.Combine(IntermediateOutputPath,
                                                             Path.GetDirectoryName(parsedClient.SourceItemSpec),
                                                             string.Join(".",
                                                                         Path.GetFileNameWithoutExtension(parsedClient.SourceItemSpec),
                                                                         parsedClient.ClassName,
                                                                         "json"));
                    Path.GetDirectoryName(documentCacheFilePath).EnsureDirectoryExist();

                    string swaggerDocument;
                    if (File.Exists(documentCacheFilePath))
                    {
                        Log.LogMessage(MessageImportance.Normal, "  - Document available in cache");
                        swaggerDocument = File.ReadAllText(documentCacheFilePath);
                        Log.LogMessage(MessageImportance.Normal, "  - Document loaded from cache");
                    }
                    else
                    {
                        Log.LogMessage(MessageImportance.Normal,
                                       $"  - Fetching document from '{declaration.GetDocumentPath(replacer)}' in {declaration.Source} mode");
                        try
                        {
                            swaggerDocument = declaration.GetDocument(replacer);

                            Log.LogMessage(MessageImportance.Normal, "  - Document acquired. Caching...");
                            File.WriteAllText(documentCacheFilePath, swaggerDocument);
                            _cachedItems.Add(new TaskItem(documentCacheFilePath));
                            Log.LogMessage(MessageImportance.Normal, "  - Document cached");
                        }
                        catch (Exception e)
                        {
                            Log.LogErrorFromException(new Exception("Cannot fetch swagger document", e));
                            throw;
                        }
                    }

                    try
                    {
                        var code     = GenerateClientCode(parsedClient, swaggerDocument, replacer);
                        var filePath = client.GetMetadata("FullPath");
                        Log.LogMessage(MessageImportance.Normal, $"  - Saving generated code to {filePath}");
                        File.WriteAllText(filePath, code);
                    }
                    catch (Exception e)
                    {
                        Log.LogError($"Cannot generate code. Details: {e.Message}");
                        throw;
                    }
                }
                catch (Exception)
                {
                    //Nothing
                }
            }

            return(true);
        }