c# - ICustomFormatter usage -


assume have icustomformatter designed provide intrinsic support type, in case personname. of msdn docs have code similar snippet below when comes handling arg parameter.

i can conjure scenario arg null, shown test below, test arg type isn't personname looks awfully contrived.

what's more realistic scenario personnameformatter might called type isn't personname?

code

public class personnameformatter : iformatprovider, icustomformatter {     public string format(string format, object arg, iformatprovider formatprovider) {         var pn = arg personname;         if (pn != null) {             // formet it!!         }         try {             return handleotherformats(format, arg);         }         catch (formatexception e) {             throw new formatexception(string.format("the format of '{0}' invalid.", format), e);         }     }      private static string handleotherformats(string format, object arg) {         var formattable = arg iformattable;         if (formattable != null)             return formattable.tostring(format, cultureinfo.currentculture);          return arg != null ? arg.tostring() : string.empty;     } } 

test arg null (realistic)

[test] public void if_arg_is_null_then_string_empty_s_returned() {     personname pn = null;     var result = string.format("{0:g}", pn);     assert.that(result, is.equalto(string.empty)); } 

test arg wrong type (effective contrived??)

    [test]     public void if_arg_is_not_person_name_then_tostring_for_that_arg_is_returned()     {         var result = personnameformatter.instance.format("n2", 1, null);         assert.that(result, is.equalto(1.tostring("n2")));     } 


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -