private JsonTestData GetJsonFileFromFolder(string folderName, bool isStringEnum) { var folder = Path.Combine(TestDataFolder, folderName); Assert.IsTrue(Directory.Exists(folder), $"Directory {folder} not found."); var jsonTestData = new JsonTestData(); foreach (var file in Directory.GetFiles(folder).OrderBy(x => x)) { if (file.IndexOf(ExpectedResultFile, StringComparison.OrdinalIgnoreCase) >= 0) { try { var handHistoryText = File.ReadAllText(file); jsonTestData.ExpectedResult = SerializationHelper.DeserializeObject <HandHistory>(handHistoryText); } catch (Exception e) { Assert.Fail($"{ExpectedResultFile} in {folder} has not been deserialized: {e}"); } continue; } if (!Path.GetExtension(file).Equals(JsonExt)) { continue; } try { var gameRoomStateChangeText = File.ReadAllText(file); var gameRoomStateChange = isStringEnum ? JsonConvert.DeserializeObject <SCGameRoomStateChange>(gameRoomStateChangeText, new StringEnumConverter()) : JsonConvert.DeserializeObject <SCGameRoomStateChange>(gameRoomStateChangeText); jsonTestData.GameRoomStateChanges.Add(gameRoomStateChange); } catch (Exception e) { Assert.Fail($"{file} in {folder} has not been deserialized into {nameof(SCGameRoomStateChange)}: {e}"); } } Assert.That(jsonTestData.GameRoomStateChanges.Count, Is.GreaterThan(0), $"Directory {folder} must contain at least 1 file with commands."); Assert.IsNotNull(jsonTestData.ExpectedResult, $"Directory {folder} must contain proper {ExpectedResultFile}."); return(jsonTestData); }
// run the testcase in one piece of JSON static void RunJsonTestCase(string sourcename, string json) { _output.WriteLine($"Run JSON test: '{sourcename}'"); JsonTestData.SetupSingle(json); var testdata = JsonTestData.ClassTestCases; _output.WriteLine($"json test count: {testdata.Count}"); var testcases = testdata .Take(_take); var counter = 0; foreach (var testcase in testcases) { RunJsonTest(testcase.Title, testcase.Script, testcase.Inputs.Join(), testcase.TargetLevel.ToString(), testcase.FinalState, ++counter, testcase.RandomSeed); } }
// expand one piece of JSON into a set of converted test cases in a directory, create if needed static void RunJsonConvert(string path, string script, string dir) { _output.WriteLine($"Converting JSON: '{path}' to '{dir}'"); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } JsonTestData.Setup(script); var testdata = JsonTestData.ClassTestCases; _output.WriteLine($"json: '{testdata.Count}'"); var badchars = new HashSet <char>(Path.GetInvalidFileNameChars()); var counter = 0; foreach (var testcase in testdata) { var title = new string(testcase.Title.Select(c => badchars.Contains(c) ? '-' : c).ToArray()); var filename = String.Format("{0:D3}-{1}.txt", ++counter, title); var outpath = Path.Combine(dir, filename); _output.WriteLine($"title: '{testcase.Title} to {outpath}"); using (var sw = new StreamWriter(outpath)) sw.Write(JsonTestData.ConvertToScript(testcase)); } }