Exemple #1
0
        public void TestOneWayBindingWithConverter()
        {
            int bindingId;
            BindingTestObject    source    = new BindingTestObject();
            BindingTestObject    target    = new BindingTestObject();
            StringToIntConverter converter = new StringToIntConverter();

            bindingId = BindingManager.Register(source, s => s.SourceStringProperty, target,
                                                t => t.SourceIntProperty, default(string), BindingMode.OneWay, converter);

            //After the binding, changes to the source property should be converted to their integer counterparts
            //and propogated to the target
            source.SourceStringProperty = "99";
            Assert.AreEqual(target.SourceIntProperty, 99);

            //However, values that cannot be converted to an integer should cause the fallback value to be used.
            //Since no fallback value was specified, the value should be set to the default of integer type
            source.SourceStringProperty = "Toasty";
            Assert.AreEqual(target.SourceIntProperty, default(int));

            //Also, changes to the target should not affect the source in a OneWay binding
            target.SourceIntProperty = 55;
            Assert.AreNotEqual(source.SourceStringProperty, "55");

            //Disposing of the bindings should cease updates to stop propogating
            BindingManager.Unregister(bindingId);

            source.SourceStringProperty = "-1";
            Assert.AreNotEqual(target.SourceIntProperty, -1);
            Assert.AreEqual(target.SourceIntProperty, 55);
        }
        public void StringToIntConverter(string value, int expectedResult)
        {
            var stringToIntConverter = new StringToIntConverter();

            var result = stringToIntConverter.Convert(value, typeof(StringToIntConverter_tests), null, CultureInfo.CurrentCulture);

            Assert.Equal(result, expectedResult);
        }
        public void IntToStringConverter(int value, string expectedResult)
        {
            var stringToIntConverter = new StringToIntConverter();

            var result = stringToIntConverter.ConvertBack(value, typeof(StringToIntConverter_tests), null,
                                                          CultureInfo.CurrentCulture);

            Assert.Equal(result, expectedResult);
        }
        public void TestBasis8()
        {
            Assert.AreEqual(0, StringToIntConverter.Parse("0", 8), "parse error");

            Assert.AreEqual(1, StringToIntConverter.Parse("1", 8), "parse error");
            Assert.AreEqual(57, StringToIntConverter.Parse("71", 8), "parse error");

            Assert.AreEqual(-1, StringToIntConverter.Parse("-1", 8), "parse error");
            Assert.AreEqual(-57, StringToIntConverter.Parse("-71", 8), "parse error");
        }
        public void Convert_TooBigInt_OverflowException()
        {
            // Arrange
            string s = "2147483648";

            // Act
            var r = StringToIntConverter.Convert(s);

            // Assert exception is thrown
        }
        public void Convert_InvalidFormat_FormatException()
        {
            // Arrange
            string s = "-456wat";

            // Act
            var r = StringToIntConverter.Convert(s);

            // Assert exception is thrown
        }
        public void Convert_Null_ArgumentException()
        {
            // Arrange
            string s = null;

            // Act
            var r = StringToIntConverter.Convert(s);

            // Assert exception is thrown
        }
        public void Convert_FiveDigitInt_Converted()
        {
            // Arrange
            var s = "12345";

            // Act
            var r = StringToIntConverter.Convert(s);

            // Assert
            Assert.AreEqual(r, 12345);
        }
        public void Convert_NegativeInt_Converted()
        {
            // Arrange
            var s = "-2147483648";

            // Act
            var r = StringToIntConverter.Convert(s);

            // Assert
            Assert.AreEqual(r, -2147483648);
        }
        public void Convert_PositiveInt_Converted()
        {
            // Arrange
            var s = "+2147483647";

            // Act
            var r = StringToIntConverter.Convert(s);

            // Assert
            Assert.AreEqual(r, 2147483647);
        }
Exemple #11
0
        static void Main(string[] args)
        {
            string sp      = @"fileTest.txt";
            string op      = @"output.txt";
            var    ng      = new TextLinesGetter();
            var    lines   = ng.GetTheLines(sp);
            var    sic     = new StringToIntConverter();
            var    numbers = sic.ConvertStringToInt(lines);
            var    lc      = new ListCreator(numbers);

            lc.MyList.Sort();
            var tw = new TextLinesWriter(lc.MyList, op);
        }
Exemple #12
0
        public void StringToDictionaryWithConverterTest()
        {
            var converter = new StringToIntConverter();
            var dict      = Splitter.On("&").WithKeyValueSeparator("=").SplitToDictionary(OriginalStrings.NormalMapString, converter);

            dict.Count.ShouldBe(5);

            dict["a"].ShouldBe(1);
            dict["b"].ShouldBe(2);
            dict["c"].ShouldBe(3);
            dict["d"].ShouldBe(4);
            dict["e"].ShouldBe(5);
        }
        public void TestBasis16()
        {
            Assert.AreEqual(0, StringToIntConverter.Parse("0", 16), "parse error");

            Assert.AreEqual(1, StringToIntConverter.Parse("1", 16), "parse error");
            Assert.AreEqual(241, StringToIntConverter.Parse("F1", 16), "parse error");
            Assert.AreEqual(983041, StringToIntConverter.Parse("F0001", 16), "parse error");
            Assert.AreEqual(Int64.MaxValue, StringToIntConverter.Parse("7FFFFFFFFFFFFFFF", 16), "parse error");

            Assert.AreEqual(-1, StringToIntConverter.Parse("-1", 16), "parse error");
            Assert.AreEqual(-241, StringToIntConverter.Parse("-F1", 16), "parse error");
            Assert.AreEqual(-983041, StringToIntConverter.Parse("-F0001", 16), "parse error");
            Assert.AreEqual(Int64.MinValue, StringToIntConverter.Parse("-8000000000000000", 16), "parse error");
        }
        public void TestBasis10()
        {
            Assert.AreEqual(0, StringToIntConverter.Parse("0", 10), "parse error");

            Assert.AreEqual(1, StringToIntConverter.Parse("1", 10), "parse error");
            Assert.AreEqual(11, StringToIntConverter.Parse("11", 10), "parse error");
            Assert.AreEqual(10001, StringToIntConverter.Parse("10001", 10), "parse error");
            Assert.AreEqual(Int64.MaxValue, StringToIntConverter.Parse(Int64.MaxValue.ToString(), 10), "parse error");
            Assert.AreEqual(UInt64.MaxValue, StringToIntConverter.Parse(UInt64.MaxValue.ToString(), 10), "parse error");

            Assert.AreEqual(-1, StringToIntConverter.Parse("-1", 10), "parse error");
            Assert.AreEqual(-11, StringToIntConverter.Parse("-11", 10), "parse error");
            Assert.AreEqual(-10001, StringToIntConverter.Parse("-10001", 10), "parse error");
            Assert.AreEqual(Int64.MinValue, StringToIntConverter.Parse(Int64.MinValue.ToString(), 10), "parse error");
            Assert.AreEqual(UInt64.MinValue, StringToIntConverter.Parse(UInt64.MinValue.ToString(), 10), "parse error");
        }
Exemple #15
0
        public void StringToKvpWithConverterTest()
        {
            var converter = new StringToIntConverter();
            var kvp       = Splitter.On("&").WithKeyValueSeparator("=").Split(OriginalStrings.NormalMapString, converter);

            // ReSharper disable once PossibleMultipleEnumeration
            kvp.Count().ShouldBe(5);

            // ReSharper disable once PossibleMultipleEnumeration
            var dict = kvp.ToDictionary(k => k.Key, v => v.Value);

            dict.Count.ShouldBe(5);

            dict["a"].ShouldBe(1);
            dict["b"].ShouldBe(2);
            dict["c"].ShouldBe(3);
            dict["d"].ShouldBe(4);
            dict["e"].ShouldBe(5);
        }
Exemple #16
0
        static void Main(string[] args)
        {
            string entered = "";

            while (!entered.ToLower().Equals("exit"))
            {
                Console.WriteLine("How many iterations to calculate Square? (Type 'Exit' to cancel)");

                entered = Console.ReadLine();
                Console.WriteLine("Entered: " + entered);
                //Console.WriteLine(entered);
                int iterations = new StringToIntConverter(entered).Success ?
                                 new StringToIntConverter(entered).Result :
                                 0;

                string resultOutput = "";
                for (int i = 1; i <= iterations; i++)
                {
                    int squared = new Squared(i).Result;
                    resultOutput += (squared).ToString() + (i == iterations ? "" : ", ");
                }
                Console.WriteLine(resultOutput.ToString());
            }
        }
Exemple #17
0
        /// <summary>
        /// Reads the athlete details xml from file and decodes it.
        /// </summary>
        /// <param name="fileName">name of xml file</param>
        /// <returns>decoded athlete's details</returns>
        public Athletes ReadAthleteData(string fileName)
        {
            Athletes athleteData = new Athletes(this.seriesConfigManager);

            if (!File.Exists(fileName))
            {
                string error = string.Format("Athlete Data file missing, one created - {0}", fileName);
                Messenger.Default.Send(
                    new HandicapErrorMessage(
                        error));
                this.logger.WriteLog(error);
                SaveAthleteData(fileName, new Athletes(this.seriesConfigManager));
            }

            try
            {
                XDocument reader             = XDocument.Load(fileName);
                XElement  rootElement        = reader.Root;
                XElement  athleteListElement = rootElement.Element(c_athleteListLabel);

                var athleteList = from Athlete in athleteListElement.Elements(c_athleteLabel)
                                  select new
                {
                    key  = (string)Athlete.Attribute(c_keyLabel),
                    name = (string)Athlete.Attribute(c_nameLabel),
                    club = (string)Athlete.Attribute(c_clubLabel),
                    predeclaredHandicap = (string)Athlete.Attribute(predeclaredHandicapLabel),
                    sex            = (string)Athlete.Attribute(c_sexLabel),
                    signedConsent  = (string)Athlete.Attribute(SignedConsentAttribute),
                    active         = (string)Athlete.Attribute(activeLabel),
                    birthYear      = (string)Athlete.Attribute(birthYearAttribute),
                    birthMonth     = (string)Athlete.Attribute(birthMonthAttribute),
                    birthDay       = (string)Athlete.Attribute(birthDayAttribute),
                    runningNumbers = from RunningNumbers in Athlete.Elements(c_runningNumbersElement)
                                     select new
                    {
                        numbers = from Numbers in RunningNumbers.Elements(c_numberElement)
                                  select new
                        {
                            number = (string)Numbers.Attribute(c_numberAttribute)
                        }
                    },
                    timeList = from TimeList in Athlete.Elements(c_appearancesLabel)
                               select new
                    {
                        time = from Time in TimeList.Elements(c_timeLabel)
                               select new
                        {
                            time = (string)Time.Attribute(c_raceTimeLabel),
                            date = (string)Time.Attribute(c_raceDateLabel)
                        }
                    }
                };

                foreach (var athlete in athleteList)
                {
                    bool signedConsent = this.ConvertBool(athlete.signedConsent);
                    bool isActive      = this.ConvertBool(athlete.active);

                    int athleteKey =
                        (int)StringToIntConverter.ConvertStringToInt(
                            athlete.key);
                    TimeType athleteTime =
                        new TimeType(
                            athlete.predeclaredHandicap);
                    SexType            athleteSex         = StringToSexType.ConvertStringToSexType(athlete.sex);
                    List <Appearances> athleteAppearances = new List <Appearances>();

                    AthleteDetails athleteDetails =
                        new AthleteDetails(
                            athleteKey,
                            athlete.name,
                            athlete.club,
                            athleteTime,
                            athleteSex,
                            signedConsent,
                            isActive,
                            athleteAppearances,
                            athlete.birthYear,
                            athlete.birthMonth,
                            athlete.birthDay,
                            this.normalisationConfigManager);

                    foreach (var runningNumbers in athlete.runningNumbers)
                    {
                        foreach (var number in runningNumbers.numbers)
                        {
                            athleteDetails.AddNewNumber(number.number);
                        }
                    }

                    foreach (var raceTimeList in athlete.timeList)
                    {
                        foreach (var raceTime in raceTimeList.time)
                        {
                            athleteDetails.AddRaceTime(new Appearances(new RaceTimeType(raceTime.time),
                                                                       new DateType(raceTime.date)));
                        }
                    }

                    athleteData.SetNewAthlete(athleteDetails);
                }
            }
            catch (Exception ex)
            {
                this.logger.WriteLog("Error reading athlete data: " + ex.ToString());

                athleteData = new Athletes(this.seriesConfigManager);
            }

            return(athleteData);
        }
Exemple #18
0
 /// <summary>
 /// Converts the position string to an integer.
 /// This assumes that the string is confirmed to be a position input.
 /// </summary>
 /// <param name="input">string to convert</param>
 /// <returns>position value, null if <paramref name="input"/> is invalid</returns>
 public static int?ConvertPositionValue(string input)
 {
     return(StringToIntConverter.ConvertStringToInt(input.Substring(1)));
 }
        static void Main(string[] args)
        {
            var s = "-2147483649";

            Console.WriteLine($"Example of converting: {StringToIntConverter.Convert(s)}");
        }
Exemple #20
0
        /// ---------- ---------- ---------- ---------- ---------- ----------
        /// <name>ReadCompleteSummaryData</name>
        /// <date>31/01/15</date>
        /// <summary>
        /// Reads the athlete details xml from file and decodes it.
        /// </summary>
        /// <param name="fileName">name of xml file</param>
        /// <returns>decoded summary data</returns>
        /// ---------- ---------- ---------- ---------- ---------- ----------
        public ISummary ReadCompleteSummaryData(string fileName)
        {
            ISummary summaryData = null;

            if (!File.Exists(fileName))
            {
                string error = $"Summary file missing, one created - {fileName}";
                Messenger.Default.Send(
                    new HandicapErrorMessage(
                        error));
                this.logger.WriteLog(error);
                SaveSummaryData(fileName, new Summary());
            }

            try
            {
                XDocument reader      = XDocument.Load(fileName);
                XElement  rootElement = reader.Root;

                XElement TotalRunnersElement  = rootElement.Element(c_RunnersLabel);
                XElement MaleRunnersElement   = rootElement.Element(c_MaleRunnersLabel);
                XElement FemaleRunnersElement = rootElement.Element(c_FemaleRunnersLabel);
                XElement YBsElement           = rootElement.Element(c_YBLabel);
                XElement PBsElement           = rootElement.Element(c_PBLabel);
                XElement FirstTimersElement   = rootElement.Element(c_FirstTimersLabel);
                XElement FastestElement       = rootElement.Element(c_FastestLabel);

                XElement FastestBoysElement  = FastestElement.Element(fastestBoysLabel);
                XElement FastestGirlsElement = FastestElement.Element(fastestGirlsLabel);


                summaryData =
                    new Summary(
                        (int)StringToIntConverter.ConvertStringToInt(MaleRunnersElement.Value),
                        (int)StringToIntConverter.ConvertStringToInt(FemaleRunnersElement.Value),
                        (int)StringToIntConverter.ConvertStringToInt(YBsElement.Value),
                        (int)StringToIntConverter.ConvertStringToInt(PBsElement.Value),
                        (int)StringToIntConverter.ConvertStringToInt(FirstTimersElement.Value));

                var boysList = from Athlete in FastestBoysElement.Elements(timeLabel)
                               select new
                {
                    key     = (string)Athlete.Attribute(keyLabel),
                    name    = (string)Athlete.Attribute(c_NameLabel),
                    minutes = (int)Athlete.Attribute(c_MinutesLabel),
                    seconds = (int)Athlete.Attribute(c_SecondsLabel),
                    date    = (string)Athlete.Attribute(dateLabel)
                };

                foreach (var athlete in boysList)
                {
                    summaryData.SetFastestBoy((int)StringToIntConverter.ConvertStringToInt(athlete.key),
                                              athlete.name,
                                              new TimeType(athlete.minutes, athlete.seconds),
                                              new DateType(athlete.date));
                }

                var girlsList = from Athlete in FastestGirlsElement.Elements(timeLabel)
                                select new
                {
                    key     = (string)Athlete.Attribute(keyLabel),
                    name    = (string)Athlete.Attribute(c_NameLabel),
                    minutes = (int)Athlete.Attribute(c_MinutesLabel),
                    seconds = (int)Athlete.Attribute(c_SecondsLabel),
                    date    = (string)Athlete.Attribute(dateLabel)
                };

                foreach (var athlete in girlsList)
                {
                    summaryData.SetFastestGirl((int)StringToIntConverter.ConvertStringToInt(athlete.key),
                                               athlete.name,
                                               new TimeType(athlete.minutes, athlete.seconds),
                                               new DateType(athlete.date));
                }
            }
            catch (Exception ex)
            {
                this.logger.WriteLog("Error reading summary data: " + ex.ToString());

                summaryData = new Summary();
            }

            return(summaryData);
        }