Skip to content
Browse files

Cleaning Up More Magic String

More Constants More Tests, Better CLRVersion Structure.
  • Loading branch information...
1 parent 50aa188 commit e05ea0711019eb775e082905124681c754be8a5e @msarchet msarchet committed Sep 30, 2012
View
26 .gitignore
@@ -1,12 +1,14 @@
-# Build Folders (you can keep bin if you'd like, to store dlls and pdbs)
-bin
-obj
-
-#VSArtifacts
-*suo
-
-# mstest test results
-TestResults
-
-*suo
-*user
+# Build Folders (you can keep bin if you'd like, to store dlls and pdbs)
+bin
+obj
+
+#VSArtifacts
+*suo
+
+# mstest test results
+TestResults
+
+*suo
+*user
+
+[Pp]ackages
View
2 SmallestDotNet/javascriptdom.ashx
@@ -17,7 +17,7 @@ public class SmallestDotNet : IHttpHandler
dr(JsonVersions.WriteAllVersions(context.Request.UserAgent));
- dr(JsonVersions.WriteDownloads(context.Request.UserAgent).Trim());
+ dr(JsonVersions.WriteDownloads().Trim());
if (context.Request.UserAgent.Contains("Mac") || context.Request.UserAgent.Contains("nix"))
View
49 SmallestDotNetLib/CLRVersion.cs
@@ -0,0 +1,49 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Drawing;
+
+namespace SmallestDotNetLib
+{
+ public struct CLRVersion
+ {
+ public int Major;
+ public int Minor;
+ public string Profile;
+ public int? ServicePack;
+ public string Url;
+
+ public CLRVersion(int major, int minor, string profile, int? servicePack, string url)
+ {
+ Major = major;
+ Minor = minor;
+ Profile = profile;
+ ServicePack = servicePack;
+ Url = url;
+ }
+
+ }
+
+ public class CLRVersions
+ {
+ public static Dictionary<string, CLRVersion> Versions = new Dictionary<string, CLRVersion>
+ {
+ {Constants.Version10Full, new CLRVersion(1, 0, "full", null, "")},
+ {Constants.Version11Full, new CLRVersion(1, 1, "full", null, Constants.Version11URL)},
+ {Constants.Version20Full, new CLRVersion(2, 0, "full", null, Constants.Version20URL)},
+ {Constants.Version30Full, new CLRVersion(3, 0, "full", null, Constants.Version30URL)},
+ {Constants.Version35Full, new CLRVersion(3, 5, "full", null, "")},
+ {Constants.Version35SP1Client, new CLRVersion(3, 5, "client", 1, Constants.Version35SP1ClientURL)},
+ {Constants.Version35SP1Full, new CLRVersion(3, 5, "full", 1, Constants.Version35SP1FullURL)},
+ {Constants.Version40Client, new CLRVersion(4, 0, "client", null, Constants.Version40ClientURL)},
+ {Constants.Version40Full, new CLRVersion(4, 0, "full", null, Constants.Version40FullURL)}
+ };
+
+ public static Dictionary<string, CLRVersion> GetDownloadableVersions()
+ {
+ return Versions.Where(pair => pair.Value.Url != "").ToDictionary(pair => pair.Key, pair => pair.Value);
+ }
+ }
+}
View
11 SmallestDotNetLib/Constants.cs
@@ -3,7 +3,7 @@
using System.Web;
/// <summary>
-/// Summary description for Constants
+/// A Place for the Strings
/// </summary>
public static class Constants
{
@@ -33,4 +33,13 @@ public static class Constants
public const string Version11Full = ".NET CLR 1.1";
public const string Version10Full = ".NET CLR 1.0";
+ public const string Version11URL = "http://www.microsoft.com/downloads/details.aspx?FamilyID=a8f5654f-088e-40b2-bbdb-a83353618b38&DisplayLang=en";
+ public const string Version20URL = "http://www.microsoft.com/downloads/details.aspx?familyid=5B2C0358-915B-4EB5-9B1D-10E506DA9D0F&displaylang=en";
+ public const string Version30URL = "http://www.microsoft.com/downloads/details.aspx?FamilyId=10CC340B-F857-4A14-83F5-25634C3BF043&displaylang=en";
+ public const string Version35SP1FullURL = "http://www.microsoft.com/en-us/download/details.aspx?id=22";
+ public const string Version35SP1ClientURL = "http://www.microsoft.com/downloads/details.aspx?FamilyId=8CEA6CD1-15BC-4664-B27D-8CEBA808B28B&displaylang=en";
+ public const string Version40FullURL = "http://www.microsoft.com/downloads/details.aspx?FamilyID=9f5e8774-c8dc-4ff6-8285-03a4c387c0db&displaylang=en";
+ public const string Version40ClientURL = "http://www.microsoft.com/downloads/details.aspx?FamilyID=68a7173d-7ee5-4213-a06f-f2e943ec9249&displaylang=en";
+ public const string Version45URL = "http://www.microsoft.com/en-us/download/details.aspx?id=30653";
+
}
View
69 SmallestDotNetLib/JsonVersions.cs
@@ -89,55 +89,30 @@ public static string WriteAllVersions(string userAgent)
}
- public static string WriteDownloads(string userAgent)
+ private static string WriteDownloadVersion(CLRVersion version)
{
- return @"
- SmallestDotNet.downloadableVersions =
- [{
- major: 4,
- minor: 0,
- profile: 'client',
- servicePack: null,
- url: 'http://www.microsoft.com/downloads/details.aspx?FamilyID=68a7173d-7ee5-4213-a06f-f2e943ec9249&displaylang=en'
- },{
- major: 4,
- minor: 0,
- profile: 'full',
- servicePack: null,
- url: 'http://www.microsoft.com/downloads/details.aspx?FamilyID=9f5e8774-c8dc-4ff6-8285-03a4c387c0db&displaylang=en'
- },{
- major: 3,
- minor: 5,
- profile: 'client',
- servicePack: 1,
- url: 'http://www.microsoft.com/downloads/details.aspx?FamilyId=8CEA6CD1-15BC-4664-B27D-8CEBA808B28B&displaylang=en'
- },{
- major: 3,
- minor: 5,
- profile: 'full',
- servicePack: 1,
- url: 'http://go.microsoft.com/fwlink/?LinkId=124150'
- },{
- major: 3,
- minor: 0,
- profile: 'full',
- servicePack: 1,
- url: 'http://www.microsoft.com/downloads/details.aspx?FamilyId=10CC340B-F857-4A14-83F5-25634C3BF043&displaylang=en'
- },{
- major: 2,
- minor: 0,
- profile: 'full',
- servicePack: 2,
- url: 'http://www.microsoft.com/downloads/details.aspx?familyid=5B2C0358-915B-4EB5-9B1D-10E506DA9D0F&displaylang=en'
- },{
- major: 1,
- minor: 1,
- profile: 'full',
- servicePack: 1,
- url: 'http://www.microsoft.com/downloads/details.aspx?FamilyID=a8f5654f-088e-40b2-bbdb-a83353618b38&DisplayLang=en'
- }];
- ";
+ return String.Format(@"{{
+ major: {0},
+ minor: {1},
+ profile: '{2}',
+ servicePack: {3},
+ url: '{4}'
+ }}",
+ version.Major,
+ version.Minor,
+ version.Profile,
+ version.ServicePack.HasValue ? version.ServicePack.Value.ToString() : "null",
+ version.Url);
+ }
+ public static string WriteDownloads()
+ {
+ var DownloadableVersions = CLRVersions.GetDownloadableVersions().Select(p => JsonVersions.WriteDownloadVersion(p.Value)).ToList();
+
+ return String.Format(@"SmallestDotNet.downloadableVersions = [{0}];", String.Join(",", DownloadableVersions.ToArray()));
+
}
}
+
+
}
View
7 SmallestDotNetLib/SmallestDotNetLib.csproj
@@ -31,6 +31,9 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
+ <Reference Include="Newtonsoft.Json">
+ <HintPath>..\packages\Newtonsoft.Json.4.5.9\lib\net40\Newtonsoft.Json.dll</HintPath>
+ </Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
@@ -40,11 +43,15 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
+ <Compile Include="CLRVersion.cs" />
<Compile Include="Constants.cs" />
<Compile Include="Helpers.cs" />
<Compile Include="JsonVersions.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
+ <ItemGroup>
+ <None Include="packages.config" />
+ </ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
View
4 SmallestDotNetLib/packages.config
@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="utf-8"?>
+<packages>
+ <package id="Newtonsoft.Json" version="4.5.9" targetFramework="net45" />
+</packages>
View
68 SmallestTest/JavascriptDomTests.cs
@@ -1,6 +1,7 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SmallestDotNetLib;
+using System.Linq;
namespace SmallestTest
{
@@ -18,6 +19,16 @@ private string generateLatestVersionString(int major, int minor, string profile,
}
[TestMethod]
+ public void WriteOnlyVersionsThatHaveLinksForDownload()
+ {
+ var downloadableVersions = CLRVersions.GetDownloadableVersions();
+
+ Assert.IsTrue(downloadableVersions.All(pair => pair.Value.Url != ""));
+
+ }
+
+
+ [TestMethod]
public void Write40AsLatestVersion()
{
var userAgent = ".NET 4.0E";
@@ -125,5 +136,62 @@ public void WriteNullAsLatestVersion()
Assert.AreEqual(expectedVersion, actualVersion);
}
+
+
+ //This test is very white space sensitive currently.
+ //Needs to be cleaned up where it is only looking at the actual JSON objects
+ [TestMethod]
+ public void WritesCorrectDownloadVersionJson()
+ {
+
+ var expected = @"SmallestDotNet.downloadableVersions = [{
+ major: 1,
+ minor: 1,
+ profile: 'full',
+ servicePack: null,
+ url: 'http://www.microsoft.com/downloads/details.aspx?FamilyID=a8f5654f-088e-40b2-bbdb-a83353618b38&DisplayLang=en'
+ },{
+ major: 2,
+ minor: 0,
+ profile: 'full',
+ servicePack: null,
+ url: 'http://www.microsoft.com/downloads/details.aspx?familyid=5B2C0358-915B-4EB5-9B1D-10E506DA9D0F&displaylang=en'
+ },{
+ major: 3,
+ minor: 0,
+ profile: 'full',
+ servicePack: null,
+ url: 'http://www.microsoft.com/downloads/details.aspx?FamilyId=10CC340B-F857-4A14-83F5-25634C3BF043&displaylang=en'
+ },{
+ major: 3,
+ minor: 5,
+ profile: 'client',
+ servicePack: 1,
+ url: 'http://www.microsoft.com/downloads/details.aspx?FamilyId=8CEA6CD1-15BC-4664-B27D-8CEBA808B28B&displaylang=en'
+ },{
+ major: 3,
+ minor: 5,
+ profile: 'full',
+ servicePack: 1,
+ url: 'http://www.microsoft.com/en-us/download/details.aspx?id=22'
+ },{
+ major: 4,
+ minor: 0,
+ profile: 'client',
+ servicePack: null,
+ url: 'http://www.microsoft.com/downloads/details.aspx?FamilyID=68a7173d-7ee5-4213-a06f-f2e943ec9249&displaylang=en'
+ },{
+ major: 4,
+ minor: 0,
+ profile: 'full',
+ servicePack: null,
+ url: 'http://www.microsoft.com/downloads/details.aspx?FamilyID=9f5e8774-c8dc-4ff6-8285-03a4c387c0db&displaylang=en'
+ }];";
+
+ var actual = JsonVersions.WriteDownloads();
+
+ Assert.AreEqual(expected, actual);
+
+ }
}
}

0 comments on commit e05ea07

Please sign in to comment.
Something went wrong with that request. Please try again.