public void CompareToWorksCorrectly(KspVersion v1, KspVersion v2, int comparison) { // Act var genericCompareTo = v1.CompareTo(v2); var nonGenericCompareTo = v1.CompareTo((object)v2); var lessThanOperator = v1 < v2; var lessThanOrEqualOperator = v1 <= v2; var greaterThanOperator = v1 > v2; var greaterThanOrEqualOperator = v1 >= v2; var reverseGenericCompareTo = v2.CompareTo(v1); var reverseNonGenericCompareTo = v2.CompareTo((object)v1); var reverseLessThanOperator = v2 < v1; var reverseLessThanOrEqualOperator = v2 <= v1; var reverseGreaterThanOperator = v2 > v1; var reverseGreaterThanOrEqualOperator = v2 >= v1; // Assert Assert.AreEqual(Math.Sign(comparison), Math.Sign(genericCompareTo)); Assert.AreEqual(Math.Sign(comparison), Math.Sign(nonGenericCompareTo)); Assert.AreEqual(comparison < 0, lessThanOperator); Assert.AreEqual(comparison <= 0, lessThanOrEqualOperator); Assert.AreEqual(comparison > 0, greaterThanOperator); Assert.AreEqual(comparison >= 0, greaterThanOrEqualOperator); Assert.AreEqual(-Math.Sign(comparison), Math.Sign(reverseGenericCompareTo)); Assert.AreEqual(-Math.Sign(comparison), Math.Sign(reverseNonGenericCompareTo)); Assert.AreEqual(comparison > 0, reverseLessThanOperator); Assert.AreEqual(comparison >= 0, reverseLessThanOrEqualOperator); Assert.AreEqual(comparison < 0, reverseGreaterThanOperator); Assert.AreEqual(comparison <= 0, reverseGreaterThanOrEqualOperator); }
/// <summary> /// Create a new fake KSP instance /// </summary> /// <param name="new_name">The name for the new instance.</param> /// <param name="new_path">The loaction of the new instance.</param> /// <param name="version">The version of the new instance. Should have a build number.</param> /// <param name="dlcVersion">The version of the DLC. Null if DLC should be faked.</param> public void FakeInstance(string new_name, string new_path, KspVersion version, string dlcVersion = null) { if (!version.InBuildMap()) { throw new IncorrectKSPVersionKraken(String.Format("The specified KSP version is not a known version: {0}", version.ToString())); } if (Directory.Exists(new_path) && (Directory.GetFiles(new_path).Length != 0 || Directory.GetDirectories(new_path).Length != 0)) { throw new BadInstallLocationKraken("The specified folder already exists and is not empty."); } try { log.DebugFormat("Creating folder structure and text files at {0} for KSP version {1}", Path.GetFullPath(new_path), version.ToString()); // Create a KSP root directory, containing a GameData folder, a buildID.txt/buildID64.txt and a readme.txt Directory.CreateDirectory(new_path); Directory.CreateDirectory(Path.Combine(new_path, "GameData")); Directory.CreateDirectory(Path.Combine(new_path, "Ships")); Directory.CreateDirectory(Path.Combine(new_path, "Ships", "VAB")); Directory.CreateDirectory(Path.Combine(new_path, "Ships", "SPH")); Directory.CreateDirectory(Path.Combine(new_path, "Ships", "@thumbs")); Directory.CreateDirectory(Path.Combine(new_path, "Ships", "@thumbs", "VAB")); Directory.CreateDirectory(Path.Combine(new_path, "Ships", "@thumbs", "SPH")); // Don't write the buildID.txts if we have no build, otherwise it would be -1. if (version.IsBuildDefined) { File.WriteAllText(Path.Combine(new_path, "buildID.txt"), String.Format("build id = {0}", version.Build)); File.WriteAllText(Path.Combine(new_path, "buildID64.txt"), String.Format("build id = {0}", version.Build)); } // Create the readme.txt WITHOUT build number. File.WriteAllText(Path.Combine(new_path, "readme.txt"), String.Format("Version {0}", new KspVersion(version.Major, version.Minor, version.Patch).ToString())); // If a installed DLC should be simulated, we create the needed folder structure and the readme.txt if (!String.IsNullOrEmpty(dlcVersion) && version.CompareTo(new KspVersion(1, 4, 0)) >= 0) { Directory.CreateDirectory(Path.Combine(new_path, "GameData", "SquadExpansion", "MakingHistory")); File.WriteAllText( Path.Combine(new_path, "GameData", "SquadExpansion", "MakingHistory", "readme.txt"), String.Format("Version {0}", dlcVersion)); } // Add the new instance to the registry KSP new_instance = new KSP(new_path, new_name, User); AddInstance(new_instance); } // Thrown by AddInstance() if created instance is not valid. // Thrown f.e. if a write operation didn't complete for unknown reasons. catch (NotKSPDirKraken kraken) { throw kraken; } }