コード例 #1
0
        public void ToStringTest()
        {
            string barcode = string.Format(
                "{0}{1}{2}{3}{4}{1}{5}",
                EnumTextAttribute.GetText(IdField.Language),
                PaperBallotIdentifier.SepMinor,
                1,
                PaperBallotIdentifier.SepMajor,
                EnumTextAttribute.GetText(IdField.Code),
                2);

            PaperBallotIdentifier identifier =
                PaperBallotIdentifier.Parse(barcode);

            string toString = identifier.ToString(),
                   field1   = string.Format(
                "{0}{1}{2}",
                EnumTextAttribute.GetText(IdField.Language),
                PaperBallotIdentifier.SepMinor,
                1),
                   field2 = string.Format(
                "{0}{1}{2}",
                EnumTextAttribute.GetText(IdField.Code),
                PaperBallotIdentifier.SepMinor,
                2);

            Assert.IsTrue(toString.IndexOf(field1) > -1);
            Assert.IsTrue(toString.IndexOf(field2) > -1);

            identifier = new PaperBallotIdentifier();
            Assert.AreEqual(string.Empty, identifier.ToString());
        }
コード例 #2
0
        public void ToStringFailsWithoutCodeAndLanguageFieldsTest()
        {
            string barcode = string.Format(
                "{0}{1}{2}{3}{4}{1}{5}",
                EnumTextAttribute.GetText(IdField.BallotTypeId),
                PaperBallotIdentifier.SepMinor,
                1,
                PaperBallotIdentifier.SepMajor,
                EnumTextAttribute.GetText(IdField.Precinct),
                2);

            PaperBallotIdentifier identifier =
                PaperBallotIdentifier.Parse(barcode);

            string toString = identifier.ToString(),
                   field1   = string.Format(
                "{0}{1}{2}",
                EnumTextAttribute.GetText(IdField.BallotTypeId),
                PaperBallotIdentifier.SepMinor,
                1),
                   field2 = string.Format(
                "{0}{1}{2}",
                EnumTextAttribute.GetText(IdField.Precinct),
                PaperBallotIdentifier.SepMinor,
                2);

            Assert.IsTrue(toString.IndexOf(field1) > -1);
            Assert.IsTrue(toString.IndexOf(field2) > -1);
        }
コード例 #3
0
        public void ParseOk_Get_SetGet()
        {
            IdField[] fields  = (IdField[])Enum.GetValues(typeof(IdField));
            string    barcode = string.Empty;

            // builds a barcode string with all fields
            for (int i = 0; i < fields.Length; i++)
            {
                if (i > 0)
                {
                    barcode += PaperBallotIdentifier.SepMajor;
                }

                barcode += EnumTextAttribute.GetText(fields[i])
                           + PaperBallotIdentifier.SepMinor + i;
            }

            // parse the barcode string and build a new paper ballot identifier
            // instance
            PaperBallotIdentifier objId = PaperBallotIdentifier.Parse(barcode);

            // get each field and verify it was properly parsed
            for (int i = 0; i < fields.Length; i++)
            {
                Assert.AreEqual(objId.Get(fields[i]), i.ToString());
            }

            // set a new value to each field and verify again
            for (int i = 0, val; i < fields.Length; i++)
            {
                val = fields.Length + i;
                objId.Set(fields[i], val);
                Assert.AreEqual(objId.Get(fields[i]), val);
            }
        }
コード例 #4
0
        public void ParseCatchesException()
        {
            string barcode = "badidentifier" + PaperBallotIdentifier.SepMinor
                             + "something";

            PaperBallotIdentifier identifier =
                PaperBallotIdentifier.Parse(barcode);

            // no fields
            Assert.IsFalse(identifier.HasFields);

            // 1 field ok, 1 not ok (discarded)
            barcode = string.Format(
                barcode + "{0}{1}{2}{3}",
                PaperBallotIdentifier.SepMajor,
                EnumTextAttribute.GetText(IdField.Precinct),
                PaperBallotIdentifier.SepMinor,
                1);
            identifier = PaperBallotIdentifier.Parse(barcode);
            Assert.IsTrue(identifier.HasFields);

            // no fields either
            barcode    = "sometext";
            identifier = PaperBallotIdentifier.Parse(barcode);
            Assert.IsFalse(identifier.HasFields);
        }
コード例 #5
0
        /// <summary>
        ///     Returns a <see cref="T:System.String"/> that represents the
        ///     current <see cref="PaperBallotIdentifier"/>.
        /// </summary>
        /// <returns>
        ///     A <see cref="T:System.String"/> that represents the current
        ///     <see cref="T:System.Object"/>.
        /// </returns>
        /// <externalUnit/>
        /// <revision revisor="dev11" date="12/24/2008" version="1.0.0.0">
        ///     Member Created
        /// </revision>
        /// <revision revisor="dev06" date="1/10/2009" version="1.0.0.0">
        ///     Updated so that the card identifer only shows the language and
        ///     the code - this will need to be revised when we figure out how
        ///     the mask will work
        /// </revision>
        /// <revision revisor="dev11" date="02/27/2009" version="1.0.8.1001">
        ///     the restriction for including only [language] and [code] has
        ///     been removed
        /// </revision>
        public override string ToString()
        {
            string identifier = string.Empty, field;

            IdField[] fields = Enum.GetValues(typeof(IdField)) as IdField[];
            for (int i = 0; i < fields.Length; i = i + 1)
            {
                if (this.fieldMap.ContainsKey(fields[i]) == true)
                {
                    if (identifier.Length > 0)
                    {
                        identifier = identifier + SepMajor;
                    }

                    if (fields[i] == IdField.ControlBallot)
                    {
                        field =
                            Convert.ToInt32(
                                this.fieldMap[fields[i]]).ToString();
                    }
                    else
                    {
                        field = this.fieldMap[fields[i]].ToString();
                    }

                    identifier = identifier
                                 + EnumTextAttribute.GetText(fields[i])
                                 + SepMinor + field;
                }
            }

            return(identifier);
        }
コード例 #6
0
        /// <summary>
        ///     Parses the specified identifier.
        ///     the string format is as follows:
        ///     key1:value1|key2:value2|key3:value3|...
        ///     IMPORTANT: this method depends on EnumTextAttribute class and
        ///     hence all IdFields texts have to be used first.
        /// </summary>
        /// <param name="identifier">The identifier.</param>
        /// <returns>
        ///     A <see cref="PaperBallotIdentifier" /> containing the specified
        ///     identifier.
        /// </returns>
        /// <externalUnit/>
        /// <revision revisor="dev11" date="12/24/2008" version="1.0.0.0">
        ///     Member Created
        /// </revision>
        public static PaperBallotIdentifier Parse(string identifier)
        {
            PaperBallotIdentifier pbiId = new PaperBallotIdentifier();

            string[] pairs = identifier.Split(SepMajor),
            pair;
            IdField key;

            foreach (string strPair in pairs)
            {
                pair = strPair.Split(SepMinor);
                if (pair.Length != 2)
                {
                    continue;
                }

                try
                {
                    key = (IdField)EnumTextAttribute.GetValue(pair[0]);
                    pbiId.Set(key, pair[1]);
                }
                catch (Exception)
                {
                    // simply ignore the exception and drop the current field
                    // since the field is not supported or is mispelled
                }
            }

            return(pbiId);
        }
コード例 #7
0
 public void SetupEnumTests()
 {
     /* by calling GetText first, the hashtable is populated for the
      * tests */
     EnumTest myEnum = EnumTest.EnumTypeWithTextAttribute;
     string   myText = EnumTextAttribute.GetText(myEnum);
 }
コード例 #8
0
 public void TestSetup()
 {
     IdField[] fields = (IdField[])Enum.GetValues(typeof(IdField));
     foreach (IdField field in fields)
     {
         EnumTextAttribute.GetText(field);
     }
 }
コード例 #9
0
        public void GetEnumText()
        {
            EnumTest myEnum = EnumTest.EnumTypeWithTextAttribute;
            string   myText = EnumTextAttribute.GetText(myEnum);

            Assert.AreEqual("Testing Enum Text Attributes", myText);

            myEnum = EnumTest.EnumTypeWithNoTextAttribute;
            myText = EnumTextAttribute.GetText(myEnum);
            Assert.IsNull(myText);
        }
コード例 #10
0
        public void GetEnumValue()
        {
            string myText = "Testing Enum Text Attributes";
            Enum   myEnum = EnumTextAttribute.GetValue(myText);

            Assert.AreEqual(myEnum, EnumTest.EnumTypeWithTextAttribute);

            myEnum = EnumTextAttribute.GetValue("some text here");
            Assert.IsNull(myEnum);

            myEnum = EnumTextAttribute.GetValue("Another text here");
            Assert.IsNull(myEnum);

            EnumTextAttribute.GetText(EnumTest.EnumTypeWithAnotherText);
            myEnum = EnumTextAttribute.GetValue("Another text here");
            Assert.IsNotNull(myEnum);
            Assert.AreNotEqual(myEnum, EnumTest.EnumTypeWithTextAttribute);
            Assert.AreNotEqual(myEnum, EnumTest.EnumTypeWithNoTextAttribute);
            Assert.AreEqual(myEnum, EnumTest.EnumTypeWithAnotherText);
        }