|
| 1 | +using ImperatorToCK3.CK3.Characters; |
| 2 | +using ImperatorToCK3.CommonUtils.Genes; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Linq; |
| 5 | +using System.Text; |
| 6 | +using Xunit; |
| 7 | + |
| 8 | +namespace ImperatorToCK3.UnitTests.CK3.Characters; |
| 9 | + |
| 10 | +public class DNATests { |
| 11 | + private static DNA CreateSampleDNA( |
| 12 | + out Dictionary<string, DNAColorGeneValue> colorDict, |
| 13 | + out Dictionary<string, DNAGeneValue> morphDict, |
| 14 | + out Dictionary<string, DNAAccessoryGeneValue> accessoryDict |
| 15 | + ) { |
| 16 | + colorDict = new Dictionary<string, DNAColorGeneValue> { |
| 17 | + { |
| 18 | + "hair_color", |
| 19 | + new DNAColorGeneValue { X = 1, Y = 2, XRecessive = 3, YRecessive = 4 } |
| 20 | + } |
| 21 | + }; |
| 22 | + morphDict = new Dictionary<string, DNAGeneValue> { |
| 23 | + { |
| 24 | + "gene_head_height", |
| 25 | + new DNAGeneValue { TemplateName = "tmpl_dom", IntSliderValue = 1, TemplateRecessiveName = "tmpl_rec", IntSliderValueRecessive = 2 } |
| 26 | + } |
| 27 | + }; |
| 28 | + var wb = new WeightBlock(); |
| 29 | + wb.AddObject("obj_a", 1); |
| 30 | + wb.AddObject("obj_b", 1); |
| 31 | + accessoryDict = new Dictionary<string, DNAAccessoryGeneValue> { |
| 32 | + { "hairstyles", new DNAAccessoryGeneValue("tmpl_acc", "obj_a", wb) } |
| 33 | + }; |
| 34 | + |
| 35 | + return new DNA("test_dna", colorDict, morphDict, accessoryDict); |
| 36 | + } |
| 37 | + |
| 38 | + [Fact] |
| 39 | + public void IdPropertyReturnsSuppliedId() { |
| 40 | + var dna = new DNA( |
| 41 | + "my_id", |
| 42 | + new Dictionary<string, DNAColorGeneValue>(), |
| 43 | + new Dictionary<string, DNAGeneValue>(), |
| 44 | + new Dictionary<string, DNAAccessoryGeneValue>() |
| 45 | + ); |
| 46 | + Assert.Equal("my_id", dna.Id); |
| 47 | + } |
| 48 | + |
| 49 | + [Fact] |
| 50 | + public void AccessoryDNAValuesReturnsExpectedEntries() { |
| 51 | + var dna = CreateSampleDNA(out _, out _, out var accessoryInput); |
| 52 | + |
| 53 | + Assert.Single(dna.AccessoryDNAValues); |
| 54 | + Assert.True(dna.AccessoryDNAValues.ContainsKey("hairstyles")); |
| 55 | + var v = dna.AccessoryDNAValues["hairstyles"]; |
| 56 | + Assert.Equal("tmpl_acc", v.TemplateName); |
| 57 | + Assert.Equal("obj_a", v.ObjectName); |
| 58 | + |
| 59 | + // Mutate original dictionary; dna should not change (constructor copies). |
| 60 | + accessoryInput["beards"] = v; |
| 61 | + Assert.Single(dna.AccessoryDNAValues); |
| 62 | + Assert.DoesNotContain("beards", dna.AccessoryDNAValues.Keys); |
| 63 | + } |
| 64 | + |
| 65 | + [Fact] |
| 66 | + public void DNALinesCombinesAllGeneTypes() { |
| 67 | + var dna = CreateSampleDNA(out var color, out var morph, out var accessory); |
| 68 | + |
| 69 | + var lines = dna.DNALines.ToList(); |
| 70 | + Assert.Equal(3, lines.Count); |
| 71 | + |
| 72 | + Assert.Contains("hair_color={ 1 2 3 4 }", lines); |
| 73 | + Assert.Contains("gene_head_height={ \"tmpl_dom\" 1 \"tmpl_rec\" 2 }", lines); |
| 74 | + // For object "obj_a" being the first entry in weight block, slider=0. |
| 75 | + Assert.Contains("hairstyles={ \"tmpl_acc\" 0 \"tmpl_acc\" 0 }", lines); |
| 76 | + |
| 77 | + // Mutate source dicts; dna should not reflect these changes. |
| 78 | + color["skin_color"] = new DNAColorGeneValue { X = 9, Y = 9, XRecessive = 9, YRecessive = 9 }; |
| 79 | + morph["gene_age"] = new DNAGeneValue { TemplateName = "age", IntSliderValue = 5, TemplateRecessiveName = "age", IntSliderValueRecessive = 5 }; |
| 80 | + accessory["beards"] = accessory["hairstyles"]; |
| 81 | + |
| 82 | + var linesAfter = dna.DNALines.ToList(); |
| 83 | + Assert.Equal(3, linesAfter.Count); |
| 84 | + Assert.DoesNotContain(linesAfter, l => l.StartsWith("skin_color=")); |
| 85 | + Assert.DoesNotContain(linesAfter, l => l.StartsWith("gene_age=")); |
| 86 | + Assert.DoesNotContain(linesAfter, l => l.StartsWith("beards=")); |
| 87 | + } |
| 88 | + |
| 89 | + [Fact] |
| 90 | + public void ConstructorCopiesInputDictionaries() { |
| 91 | + var dna = CreateSampleDNA(out var color, out var morph, out var accessory); |
| 92 | + |
| 93 | + // Replace entire dictionaries' contents after construction. |
| 94 | + color.Clear(); |
| 95 | + morph.Clear(); |
| 96 | + accessory.Clear(); |
| 97 | + |
| 98 | + // DNA should still expose original values. |
| 99 | + Assert.Single(dna.AccessoryDNAValues); |
| 100 | + Assert.Equal(3, dna.DNALines.Count()); |
| 101 | + } |
| 102 | + |
| 103 | + [Fact] |
| 104 | + public void WriteGenesProducesExpectedBlockWithEntries() { |
| 105 | + var dna = CreateSampleDNA(out _, out _, out _); |
| 106 | + var sb = new StringBuilder(); |
| 107 | + dna.WriteGenes(sb); |
| 108 | + |
| 109 | + var output = sb.ToString(); |
| 110 | + // Normalize newlines for assertions. |
| 111 | + var lines = output.Replace("\r\n", "\n").Split('\n'); |
| 112 | + // Opening and closing braces present |
| 113 | + Assert.Contains("\t\tgenes={", lines); |
| 114 | + Assert.Contains("\t\t}", lines); |
| 115 | + |
| 116 | + // Three inner lines written, each indented once more |
| 117 | + var inner = lines.Where(l => l.StartsWith("\t\t\t")).ToList(); |
| 118 | + Assert.Equal(3, inner.Count); |
| 119 | + Assert.Contains("\t\t\thair_color={ 1 2 3 4 }", inner); |
| 120 | + Assert.Contains("\t\t\tgene_head_height={ \"tmpl_dom\" 1 \"tmpl_rec\" 2 }", inner); |
| 121 | + Assert.Contains("\t\t\thairstyles={ \"tmpl_acc\" 0 \"tmpl_acc\" 0 }", inner); |
| 122 | + } |
| 123 | + |
| 124 | + [Fact] |
| 125 | + public void WriteGenesWritesEmptyBlockWhenNoGenes() { |
| 126 | + var dna = new DNA( |
| 127 | + "empty", |
| 128 | + new Dictionary<string, DNAColorGeneValue>(), |
| 129 | + new Dictionary<string, DNAGeneValue>(), |
| 130 | + new Dictionary<string, DNAAccessoryGeneValue>() |
| 131 | + ); |
| 132 | + var sb = new StringBuilder(); |
| 133 | + dna.WriteGenes(sb); |
| 134 | + |
| 135 | + var output = sb.ToString(); |
| 136 | + var lines = output.Replace("\r\n", "\n").Split('\n'); |
| 137 | + Assert.Contains("\t\tgenes={", lines); |
| 138 | + Assert.Contains("\t\t}", lines); |
| 139 | + Assert.DoesNotContain(lines, l => l.StartsWith("\t\t\t")); |
| 140 | + } |
| 141 | +} |
0 commit comments