Click here to Skip to main content
15,881,380 members
Articles / Programming Languages / C#

Roslyn CTP: Three Introductory Projects

Rate me:
Please Sign up or sign in to vote.
4.92/5 (21 votes)
8 May 2012CPOL18 min read 75.6K   1K   42  
An introduction to the Roslyn CTP
using CodeGenerator;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;

namespace CodeGeneratorTest
{
    
    
    /// <summary>
    ///This is a test class for EnumerableExtensionsTest and is intended
    ///to contain all EnumerableExtensionsTest Unit Tests
    ///</summary>
    [TestClass()]
    public class EnumerableExtensionsTest {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext {
            get {
                return testContextInstance;
            }
            set {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        [TestMethod()]
        public void CodeGeneratorTest() {
            var vehicles = new HashSet<Vehicle> { 
                new Car() { Speed = 160 }, new Car() { Speed = 120 }, new Car() { Speed = 230 },
                new Ship { Speed = 80 }, new Ship() { Speed = 60 }, new Ship() { Speed = 110 },
                new Plane { Speed = 420 }, new Plane() { Speed = 840 }, new Plane() { Speed = 720 }
            };

            Assert.AreEqual(vehicles.Average((Car c) => c.Speed),
                            vehicles.OfType<Car>().Average(c => c.Speed));

            Assert.AreEqual(vehicles.First((Car c) => c.Speed < 150),
                            vehicles.OfType<Car>().First(c => c.Speed < 150));

            Assert.AreEqual(vehicles.All((Ship s) => s.Speed < 100),
                            vehicles.OfType<Ship>().All(s => s.Speed < 100));

            Assert.IsTrue(Enumerable.SequenceEqual(
                            vehicles.Select((Ship s) => s.Speed),
                            vehicles.OfType<Ship>().Select(s => s.Speed)));

            Assert.IsTrue(Enumerable.SequenceEqual(
                            vehicles.SkipWhile((Plane p) => p.Speed < 500),
                            vehicles.OfType<Plane>().SkipWhile(p => p.Speed < 500)));

            Assert.IsTrue(Enumerable.SequenceEqual(
                            vehicles.OrderBy((Plane p) => p.Speed),
                            vehicles.OfType<Plane>().OrderBy(p => p.Speed)));
        }


        abstract class Vehicle {
            public double Speed;
        }

        class Car : Vehicle { }
        class Ship : Vehicle { }
        class Plane : Vehicle { }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Hungary Hungary
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions